import java.awt.*;
import javax.swing.*;
public class Class4 {
public static final long serialVersionUID = 1L;
public void mainMethod(int event){
JFrame f = new JFrame("Love Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,200);
f.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
if(event == 0){
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
p.setBounds(150, 0, 500, 75);
p.setPreferredSize(new Dimension(150,75));
JTextField boy = new JTextField();
boy.setMaximumSize(new Dimension(200,40));
JTextField girl = new JTextField();
girl.setMaximumSize(new Dimension(200,40));
p.add(boy);
p.add(girl);
gbc.insets = new Insets(-90,310,0,0);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p,gbc);
JPanel p3 = new JPanel(new BorderLayout());
p3.setBounds(0, 0, 150, 75);
p3.setPreferredSize(new Dimension(150,75));
Class5 c5o = new Class5();
c5o.setPreferredSize(new Dimension(150,75));
p3.add(c5o);
gbc.insets = new Insets(0,0,90,330);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p3,gbc);
JPanel p2 = new JPanel(new FlowLayout());
Class7 c7o = new Class7();
p2.add(c7o);
p2.setPreferredSize(new Dimension(300,40));
gbc.insets = new Insets(0,0,-20,0);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p2,gbc);
f.setVisible(true);
//1st
JOptionPane.showMessageDialog(null,f.isVisible());
}
if(event == 5){
JPanel p4 = new JPanel(new BorderLayout());
p4.setBounds(0,140,500,55);
Class2 c2o = new Class2();
Dimension d2 = new Dimension(500,55);
c2o.setPreferredSize(d2);
p4.setPreferredSize(d2);
p4.add(c2o);
gbc.insets = new Insets(0,0,-130,0);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p4,gbc);
f.invalidate();
f.validate();
f.repaint();
//2nd
JOptionPane.showMessageDialog(null,f.isVisible());
}
}
}
重新验证和重绘不起作用。我有一个布局管理器,所以我怀疑问题是if语句。我第一次测试f的可见性时,它返回true。我第二次测试同样的东西,它返回false。为什么我的重新验证和重绘不起作用?我需要怎么做才能让它发挥作用?
答案 0 :(得分:3)
if(event == 5){
JPanel p4 = new JPanel(new BorderLayout());
p4.setBounds(0,140,500,55);
Class2 c2o = new Class2();
Dimension d2 = new Dimension(500,55);
c2o.setPreferredSize(d2);
p4.setPreferredSize(d2);
p4.add(c2o);
gbc.insets = new Insets(0,0,-130,0);
gbc.gridx = 0;
gbc.gridy = 0;
f.add(p4,gbc);
f.invalidate();
f.validate();
f.repaint();
//2nd
JOptionPane.showMessageDialog(null,f.isVisible());
}
框架f
不可见(缺少f.setVisible(true);
),因此在这种情况下不需要重新验证和重新绘制,它对用户不可见。
还可以使用记录器或断点来测试f.isVisible()
,因为这将阻止ui。
答案 1 :(得分:3)
此代码存在一些问题,但主要问题是JFrame
引用f
的范围。
假设您正在调用此方法,就像在此main
示例中一样:
public static void main(String[] args) {
Class4 c = new Class4();
c.mainMethod(0);
c.mainMethod(5);
}
然后,您的代码在第二次调用期间创建了第二个JFrame
。
将JFrame创建移动到构造函数中,并使f
成为成员变量:
public class Class4 {
private JFrame f;
public Class4() {
f = new JFrame("Love Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,200);
f.setLayout(new GridBagLayout());
}
// ...
}
然后,您可以在mainMethod()
的后续调用中修改JFrame。
一些补充说明:
contentPane
获取JFrame
(如其他答案所示)。从Java 1.5开始,add()
的{{1}}方法直接委托给内容窗格。JFrame
添加组件时无需invalidate()
或repaint()
。 JFrame
已使组件层次结构无效。但是,当组件已经显示时(例如您的情况),您需要调用add()
。有关详细信息,请参阅java.awt.Container.add()。答案 2 :(得分:2)
我无法确切地查看您正在寻找的内容。 SSCCE真的会有所帮助。这是你在找什么?
import java.awt.*;
import javax.swing.*;
class Canv extends JComponent {
GridBagConstraints gbc;
public Canv (int event) {
gbc = new GridBagConstraints();
switch(event)
{
case 0:
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
this.setBounds(150, 0, 500, 75);
this.setPreferredSize(new Dimension(150,75));
JTextField boy = new JTextField();
boy.setMaximumSize(new Dimension(200,40));
JTextField girl = new JTextField();
girl.setMaximumSize(new Dimension(200,40));
this.add(boy);
this.add(girl);
gbc.insets = new Insets(-90,310,0,0);
gbc.gridx = 0;
gbc.gridy = 0;
this.add(this,gbc);
JPanel p3 = new JPanel(new BorderLayout());
p3.setBounds(0, 0, 150, 75);
p3.setPreferredSize(new Dimension(150,75));
//Class5 c5o = new Class5();
//c5o.setPreferredSize(new Dimension(150,75));
//p3.add(c5o);
gbc.insets = new Insets(0,0,90,330);
gbc.gridx = 0;
gbc.gridy = 0;
this.add(p3,gbc);
JPanel p2 = new JPanel(new FlowLayout());
//Class7 c7o = new Class7();
//p2.add(c7o);
p2.setPreferredSize(new Dimension(300,40));
gbc.insets = new Insets(0,0,-20,0);
gbc.gridx = 0;
gbc.gridy = 0;
this.add(p2,gbc);
break;
case 5:
JPanel p4 = new JPanel(new BorderLayout());
p4.setBounds(0,140,500,55);
//Class2 c2o = new Class2();
Dimension d2 = new Dimension(500,55);
//c2o.setPreferredSize(d2);
p4.setPreferredSize(d2);
//p4.add(c2o);
gbc.insets = new Insets(0,0,-130,0);
gbc.gridx = 0;
gbc.gridy = 0;
this.add(p4,gbc);
//this.invalidate();
this.validate();
this.repaint();
JOptionPane.showMessageDialog(null,this.isVisible());
break;
default:
break;
}
}
public void paintComponent(){
}
}
public class Class4 {
static int event;
static JFrame frame;
static Canv canvas;
public static void main(String[] args)
{
frame = new JFrame("Love Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,200);
frame.setLayout(new GridBagLayout());
//event = Integer.parseInt(args[0]);
event = 5;
canvas = new Canv(event);
frame.add(canvas);
frame.setVisible(true);
}
}
运行上面的代码将在对话框中返回true。为了在事件之间进行更改,只需在Class4中添加另一个方法来重置Canv,方法是使用不同的事件编号调用它的构造函数。