日期:2014-05-20  浏览次数:20906 次

咋修改Jpanel,的标题?
用netbeansde Jpanel,的标题可以在设计时,在border里面设置,但是在程序运行是咋修改?
------最佳解决方案--------------------
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;


public class JPanelBorderDemo extends JFrame{

private JLabel label1=null;
private JButton btn1=null;
private JLabel label2=null;
private JButton btn2=null;
private JPanel p1=null;
private JPanel p2=null;
private String[] borderName={"面板一","面板二"}; 
private Border border1=new TitledBorder("面板一");

public JPanelBorderDemo(String title){
super(title);
p1=new JPanel();
p1.setBorder(border1);

p2=new JPanel();
p2.setBorder(new TitledBorder(borderName[1]));

Container container=this.getContentPane();
container.setLayout(new FlowLayout());

label1=new JLabel("Label1");
btn1=new JButton("button1");
label1.setLabelFor(btn1);
p1.add(label1);
p1.add(btn1);

btn1.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
// p1.getBorder().
if(p1.getBorder().equals(border1))
p1.setBorder(new TitledBorder("面板三"));
else
p1.setBorder(border1);
// p1.repaint();
// p1.validate();
}

});


label2=new JLabel("Label2");
btn2=new JButton("button2");
label2.setLabelFor(btn2);
p2.add(label2);
p2.add(btn2);

container.add(p1);
container.add(p2);

this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}

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

}

引用:
正如你的例子里“面板一”,我在程序运行中能否修改?例如“增加一个按钮,点击它,“面板一”就改为“面板三”?

------其他解决方案--------------------
一眼没看过GUI的飘过
------其他解决方案--------------------
JPanel没有标题可设啊,不清楚你说的“标题是什么”,可以通过JPanel的边框设置一个字符串的显示,给你一个很简单的实例
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;


public class JPanelBorderDemo extends JFrame{

private JLabel label1=null;
private JButton btn1=null;
private JLabel label2=null;
private JButton btn2=null;
private JPanel p1=null;
private JPanel p2=null;
private String[] borderName={"面板一","面板二"}; 

public JPanelBorderDemo(String title){
super(title);
p1=new JPanel();
p1.setBorder(new TitledBorder(borderName[0]));

p2=new JPanel();
p2.setBorder(new TitledBorder(borderName[1]));

Container container=this.getContentPane();