用JAVA,Swing做的一个简单窗口,为什么两个按钮显示在Panel1里啊。。我设置的是显示在Panel2里面啊在线等 15
importjava.awt.Color;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.s...
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
private JPanel panel1, panel2;
private JButton zx, lx;
public Test() {
panel1 = new JPanel();
panel2 = new JPanel();
zx = new JButton();
lx = new JButton();
zx.setText("照相");
lx.setText("录像");
panel1.setBackground(Color.blue);
panel1.setSize(640,400);
panel2.setBackground(Color.green);
panel2.setSize(640,80);
panel2.add(lx);
panel2.add(zx);
this.add(panel1);
this.add(panel2);
this.setSize(640, 480);
this.setLocation(300, 260);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
} 展开
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
private JPanel panel1, panel2;
private JButton zx, lx;
public Test() {
panel1 = new JPanel();
panel2 = new JPanel();
zx = new JButton();
lx = new JButton();
zx.setText("照相");
lx.setText("录像");
panel1.setBackground(Color.blue);
panel1.setSize(640,400);
panel2.setBackground(Color.green);
panel2.setSize(640,80);
panel2.add(lx);
panel2.add(zx);
this.add(panel1);
this.add(panel2);
this.setSize(640, 480);
this.setLocation(300, 260);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
} 展开
4个回答
展开全部
是显示在panel2里的,因为你的JFrame窗口使用的是默认的布局方式,也就是BorderLayot布局,而且你没有指定JPanel添加的位置,所以,panel2吧panel1覆盖了而已。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
调用各种add方法之前不设置布局是不好滴……切记
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这两个Button的确是在Panel2中,看着似乎是在Panel1中,是因为Panel1这个背景色覆盖了Panel2。你可以先把Panel1给去掉,看看效果,再测试测试。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
private JPanel panel1, panel2;
private JButton zx, lx;
public Test() {
panel1 = new JPanel();
panel2 = new JPanel();
zx = new JButton();
lx = new JButton();
zx.setText("照相");
lx.setText("录像");
FlowLayout f = new FlowLayout();
f.setHgap(0);
f.setVgap(0);
this.setLayout(f);
panel1.setBackground(Color.blue);
panel1.setPreferredSize(new Dimension(640,400));
panel2.setBackground(Color.green);
panel2.setPreferredSize(new Dimension(640,80));
panel2.add(lx);
panel2.add(zx);
this.add(panel1);
this.add(panel2);
this.setSize(640, 480);
this.setLocation(300, 260);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JFrame {
private JPanel panel1, panel2;
private JButton zx, lx;
public Test() {
panel1 = new JPanel();
panel2 = new JPanel();
zx = new JButton();
lx = new JButton();
zx.setText("照相");
lx.setText("录像");
FlowLayout f = new FlowLayout();
f.setHgap(0);
f.setVgap(0);
this.setLayout(f);
panel1.setBackground(Color.blue);
panel1.setPreferredSize(new Dimension(640,400));
panel2.setBackground(Color.green);
panel2.setPreferredSize(new Dimension(640,80));
panel2.add(lx);
panel2.add(zx);
this.add(panel1);
this.add(panel2);
this.setSize(640, 480);
this.setLocation(300, 260);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询