JAVA,为什么这里的组件没有显示出来,急急急急急,求教!!
importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt.GridLayout;importja...
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RmbtextDemo extends JFrame {
JButton button1 ,button2, button3, button4, button5,button6;
JTextField textfield;
JPanel panel1,panel2;
JButton button7,button8,button9;
Container contain;
GridLayout grid;
public RmbtextDemo(){
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("+");
button8 = new JButton("-");
button9 = new JButton("=");
panel1 = new JPanel();
panel2 = new JPanel();
textfield = new JTextField(25);
textfield.setEditable(true);
Container container = getContentPane();
container.setLayout(grid);
grid = new GridLayout(3,3,5,5);
panel1.add(textfield);
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
panel2.add(button4);
panel2.add(button5);
panel2.add(button6);
panel2.add(button7);
panel2.add(button8);
panel2.add(button9);
this.setTitle("GUI");
this.setLocation(100,100);
this.setSize(300, 300);
this.setLayout(new GridLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
RmbtextDemo ex1 = new RmbtextDemo();
ex1.setVisible(true);
}
} 展开
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RmbtextDemo extends JFrame {
JButton button1 ,button2, button3, button4, button5,button6;
JTextField textfield;
JPanel panel1,panel2;
JButton button7,button8,button9;
Container contain;
GridLayout grid;
public RmbtextDemo(){
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("+");
button8 = new JButton("-");
button9 = new JButton("=");
panel1 = new JPanel();
panel2 = new JPanel();
textfield = new JTextField(25);
textfield.setEditable(true);
Container container = getContentPane();
container.setLayout(grid);
grid = new GridLayout(3,3,5,5);
panel1.add(textfield);
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
panel2.add(button4);
panel2.add(button5);
panel2.add(button6);
panel2.add(button7);
panel2.add(button8);
panel2.add(button9);
this.setTitle("GUI");
this.setLocation(100,100);
this.setSize(300, 300);
this.setLayout(new GridLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
RmbtextDemo ex1 = new RmbtextDemo();
ex1.setVisible(true);
}
} 展开
2个回答
展开全部
你的RmbtextDemo.java程序我帮你改完了,你看看吧(改动的地方见注释)
import java.awt.BorderLayout;//这里加一句脊大
import java.awt.Container;
//import java.awt.FlowLayout;//去掉这一句
import java.awt.GridLayout;
//import java.awt.LayoutManager;//去掉这一句
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RmbtextDemo extends JFrame {
JButton button1 ,button2, button3, button4, button5,button6;
JTextField textfield;
JPanel panel1,panel2;
JButton button7,button8,button9;
Container contain;
GridLayout 坦野薯grid;
public RmbtextDemo(){
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("+");
button8 = new JButton("-");
button9 = new JButton("=");
panel1 = new JPanel();
panel2 = new JPanel();
textfield = new JTextField(25);
textfield.setEditable(true);
Container container = getContentPane();
//container.setLayout(grid);//去掉这一句
grid = new GridLayout(3,3,5,5);
panel2.setLayout(grid);//这里加一句,设置panel2为网格布局
panel1.add(textfield);
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
panel2.add(button4);
panel2.add(button5);
panel2.add(button6);
panel2.add(button7);
panel2.add(button8);
panel2.add(button9);
container.add(panel1,BorderLayout.NORTH);//这里加一句,把panel1放在边界布局北面 让者因为JFrame的默认布局是边界布局,所以可以不设置
container.add(panel2,BorderLayout.CENTER);//这里加一句,把panel2放在边界布局中心 因为JFrame的默认布局是边界布局,所以可以不设置
this.setTitle("GUI");
this.setLocation(100,100);
this.setSize(300, 300);
//this.setLayout(new GridLayout());//去掉这一句
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
RmbtextDemo ex1 = new RmbtextDemo();
ex1.setVisible(true);
}
}
运行结果
更多追问追答
追问
container.add(panel1,BorderLayout.NORTH);//这里加一句,把panel1放在边界布局北面 因为JFrame的默认布局是边界布局,所以可以不设置
container.add(panel2,BorderLayout.CENTER);//这里加一句,把panel2放在边界布局中心 因为JFrame的默认布局是边界布局,所以可以不设置
那这两句是不是可以不用写
追答
必须写,因为它们是分别将panel1和panel2加到JFrame中的北面和中间的语句
否则,相应的panel1或panel2不会显示出来
我说的可以不设置,是指可以不用写 this.setLayout(new BorderLayout());//设置JFrame为边界布局 这一句可以不用写
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询