导航:首页 > 编程语言 > java中panel的使用方法

java中panel的使用方法

发布时间:2021-10-09 09:44:06

『壹』 java panel中添加panel

设置布局管理器
如果你想按顺序可以使用FlowLayout和GridLayout
当然也可以按照你想的精确位版置,调用setBounds()方法权来设置panel的位置和大小
不过这种要将布局管理设成null
例如panel.setLayout(new FlowLayout());
例如panel.setLayout(null);

『贰』 java中的panel 问题

不知道lz是否是想实现这样的效果??

下面是修改之后的程序

import java.awt.*;
import java.awt.event.*;

public class GraphicsDDA extends Frame implements ItemListener, ActionListener {
private Label label1;
private TextField startX;
private Label label2;
private TextField startY;
private Label label3;
private TextField endX;
private Label label4;
private TextField endY;
private Choice colorChoice;
private Button start;
private Panel panel;

public GraphicsDDA() {
super("DDA");
label1 = new Label("起点X");
startX = new TextField(5);
label2 = new Label("起点Y");
startY = new TextField(5);
label3 = new Label("终点X");
endX = new TextField(5);
label4 = new Label("终点Y");
endY = new TextField(5);
colorChoice = new Choice();
colorChoice.add("BLACK");
colorChoice.add("BLUE");
colorChoice.add("GREEN");
colorChoice.add("CYAN");
colorChoice.add("RED");
colorChoice.add("MAGENTA");
colorChoice.add("LIGHTGRAY");
colorChoice.add("DARKGRAY");
start = new Button("开始");
panel = new Panel();
panel.add(label1);
panel.add(startX);
panel.add(label2);
panel.add(startY);
panel.add(label3);
panel.add(endX);
panel.add(label4);
panel.add(endY);
panel.add(colorChoice);
panel.add(start);
add("Center", panel);
// panel.setSize(100, 100);
// panel.setLocation(100, 100);
// panel.setBackground(Color.white);
// this.setLayout(new BorderLayout());
this.setSize(800, 600);
//this.setBackground(Color.lightGray);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public void itemStateChanged(ItemEvent e) {
}

public void actionPerformed(ActionEvent e) {
}

public static void main(String[] args) {
new GraphicsDDA();
}
}

『叁』 java中panel怎么设置位置

Panel有个属性,visible,设为false,就不显示了,相反,设为true,就显示,这不就实现了隐藏与显示。如下:

(1)

『肆』 java中panel 面板的问题

面板实际上就是一个容器,之后可以任意在里面添加(add)或者删除(remove)内容。
例如单击某一个组件就移除这个组件,并且添加另外的组件,下面是一个具体的例子:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class WinTest3
{
public static void main(String[] args)
{

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLayout(new FlowLayout());
JPanel panel = new JPanel();
JButton button = new JButton("change");
panel.add(button);
JTextField f = new JTextField(20);
ActionListener listener = new ChangeListener(button,panel,f);
button.addActionListener(listener);//注册监听器
frame.add(panel);
frame.setVisible(true);

}
}
/*监听器,当单击按钮时,移除button按钮,加入text文本框*/
class ChangeListener implements ActionListener
{
JButton button;
JPanel panel;
JTextField text;
public ChangeListener(JButton button, JPanel panel, JTextField text)
{
super();
this.button = button;
this.panel = panel;
this.text = text;
}

@Override
public void actionPerformed(ActionEvent e)
{
if("change".equals(e.getActionCommand()))
{
panel.remove(button);
panel.add(text);
panel.updateUI();
panel.repaint();
}
}

}
备注:需要特别注意的是移除和添加组件之后,记得重画组件。

『伍』 java中的JPanel怎么用

publicstaticvoidmain(String[]args){
JFrameframe=newJFrame();
JPanelpanel=newJPanel();
JTextFieldjtf3=newJTextField("指定文本内容");
jtf3.setFont(newFont("谐体",Font.BOLD|Font.ITALIC,16));
//设置文本的水平对齐方式
jtf3.setHorizontalAlignment(JTextField.CENTER);
panel.add(jtf3);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}

『陆』 关于java中panel的问题。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JLabel;

class Test {

public static void main(String[] args) {
System.getProperties().list(System.out);
System.out.println(System.getProperty("user.home") + "/Desktop");
JLabel label = new JLabel();
label.setText("123456");
Dimension size = label.getPreferredSize();
BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_BGR);
// label.paint(image.createGraphics()); /*这个方法只有label显示的时候才能用,而且图片样子和label显示的是一模一样的*/
Graphics2D g = image.createGraphics();
g.setColor(Color.WHITE);
g.fill(new Rectangle(0,0,size.width,size.height));
g.setColor(Color.BLACK);
g.drawString(label.getText(), 0, 10);
try {
ImageIO.write(image, "jpg", new File("D:\\1.jpg"));
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
看看把,参考一下!

『柒』 Java程序页面中如何在panel里添加panel!

首先指出代码中一些不好的用法,比如:setLayout(null),
在我的java 1.4.2的环境中编译会出错。

实际上,应该使用this.getContentPane().setLayout();
这是JFrame和Frame不同的地方。

另外,在swing里面,也不推荐使用setVisible()来显示窗口,而应该使用show();

在我这里运行正常的代码如下:
/*
* Created on 2005-3-3
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.ubi.config.demo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class aaa extends JFrame
{
aaa()
{
P1 p1=new P1();
P2 p2=new P2();

this.getContentPane().setLayout(null);
setBounds(200,200,500,300);

p1.setBounds(0,0,400,300);
getContentPane().add(p1);
p2.setBounds(400,0,100,300);
getContentPane().add(p2);

}

public static void main(String[] args)
{
aaa test=new aaa();
test.show();
}
//================================================
class P1 extends JPanel
{
P1()
{
btStart=new JButton( "111 ");
add(btStart);
}
private JButton btStart;
}
class P2 extends JPanel
{
P2()
{
btStart=new JButton( "222 ");
add(btStart);
}
private JButton btStart;
}
}//:-)

『捌』 java中panel对象里面包含什么方法,可以通过什么方式可以查看到

可以,你在Frame的构造函数中将controlPanel的实例添加到下面,将mainPanel的实例添加到中间,代码大致如下:
先在controlPanel的构造函数中添加一个mainPanel类的参数,在其实例化时传进去,注意要先实例化mainPanel,如:
MainPanel mainPanel = new MainPanel();
ControlPanel controlPanel = new ControlPanel(mainPanel);
然后在Frame框架的构造函数中添加
this.add(controlPanel, Layout.SOUNTH);
this.add(mainPanel, Layout.CENTER);

这时controlPanel 对象中包含了mainPanel 对象的引用,就可以在其方法中控制mainPanel 对象了,但要注意mainPanel 对象的相关属性或方法必须是public的

『玖』 Java 中的panel()面板有什么用,怎么用。

//Jpanel和panel都为中间层容器,可显示文字、图像、绘制图形,主要功能是
在GUI中组织其他组件。但无边框,丌能做独立窗口。以Jpanel为例,给一个曾经的实例

//MyPanel.java

import java.awt.*;

import javax.swing.*;

import java.util.Random;

public class MyPanel extends JPanel {

private Circle circle[];

public MyPanel() {

setBackground(Color.black); // 面板背景色

setPreferredSize(new Dimension(600,450)); // 面板大小

int numCircle=new Random().nextInt(6)+5;

//int numCircle=circle.length;

circle=new Circle[numCircle];

for(int i=0;i<numCircle;i++){ // 创建圆的实例

Color color=new Color(new Random().nextInt(256),new Random().nextInt(256),new Random().nextInt(256));

circle[i]=new Circle(new Random().nextInt(50)+10,color,new Random().nextInt(420)+61,new Random().nextInt(320)+61);

}

}

/* 每个JPanel对象都有painComponent方法,绘制添加在容器上的组件, 在

* 组件首次显示或重新显示时被自动调用。

*/

public void paintComponent(Graphics g) {

super.paintComponent(g);

// 调用父类方法, 清空画布,绘制背景色

setFont(new Font ("Times New Roman",Font.BOLD,20)); // 设置字体

g.setColor(Color.pink);

g.drawString("Hello World!",250,200);

for(int i=0;i<circle.length;i++){

circle[i].draw(g);

}

}

}

//Circle.java

import java.awt.*;

/**此类定义了绘制圆的基本方法

*@author JF

*@version 1.0

*/

public class Circle {

// 直径和圆外接正方形左上角坐标

private int diameter,x,y;

// 指定圆的大小和位置

private Color color; // 颜色

public Circle(){

}

/**构造函数,设置圆类的属性

*@param int diameter,Color color,int x,int y

*@return none

*/

Circle(int diameter,Color color,int x,int y ){

this.diameter=diameter;

this.color=color;

this.x=x;

this.y=y;

}

/**填充圆类

*@param Graphics

*@return none

*/

public void draw(Graphics g){

g.setColor(color);

g.fillOval(x,y,diameter,diameter);

}

}

//MyCircle.java

import javax.swing.*;

public class MyCircle{

public static void main(String[] args){

JFrame f=new JFrame("Circle");

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new MyPanel());

f.pack();

}

}

『拾』 java的Jpanel方法

首先remove(int) 不是JPanel自己的方法,是它从Container类继承而来的方法
Container顾名思义,是组件的容器,它可以放许多组件。而remove(int)的参数显然不是组件的id,而是它在容器中的索引(如同数组的下标)

你要删除一个组件,可以使用 Container的remove(Component)方法,因为这个Component很容易通过事件获得,而它的索引则需要做执行一步才能获得。

阅读全文

与java中panel的使用方法相关的资料

热点内容
爱学习app主要是做什么的 浏览:407
如果网络传输数据量太大时怎么搞 浏览:438
微博群怎么升级为500 浏览:598
c写java编译器 浏览:768
oa云桌面下载文件到系统桌面 浏览:380
石油大亨安卓版 浏览:435
flash中如何导入swf文件 浏览:360
文件后面有dd是什么文件 浏览:102
三星n9108v刷机教程 浏览:689
大数据分析工资如何 浏览:461
其它文件打开的密码是多少 浏览:594
微信公众号的相关规则 浏览:516
苹果7为什么删不了软件 浏览:851
国外打印文件店叫什么名字 浏览:668
密码地球人都知道的 浏览:51
linux删除某个时间的文件 浏览:394
c盘桌面有文件就是显示不出来 浏览:45
360网络查看器在哪 浏览:778
iphone玻璃图标 浏览:694
平板拍的照片储存在什么文件夹 浏览:896

友情链接