新手提问---面板显示不出来
要拖动扩大或缩小一下窗口,那些按钮什么之类的才会出来,为什么会这样子呢?程序代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Ljjsq implements ActionListener{
JTextField textField;
public Ljjsq(){
JFrame jframe = new JFrame("LJJSQ");
JToolBar toolbar = new JToolBar();
JButton bianji = new JButton("编辑(E)");
JButton chakan = new JButton("查看(V)");
JButton bangzu = new JButton("帮助(H)");
toolbar.add(bianji);
toolbar.addSeparator();
toolbar.add(chakan);
toolbar.addSeparator();
toolbar.add(bangzu);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(400,250);
jframe.setVisible(true);
toolbar.setFloatable(false);
Container contentPane=new Container();
SpringLayout layout=new SpringLayout();
contentPane.setLayout(layout);
String score="0.0";
textField=new JTextField("sadasd",15);
contentPane.add(textField);
JButton backspace =new JButton("Backspace");
JButton ce=new JButton("CE");
JButton c=new JButton("C");
JButton mc=new JButton("MC");
JButton seven=new JButton("7");
JButton eight=new JButton("8");
JButton night=new JButton("9");
JButton divsion=new JButton("/");
JButton sqrt=new JButton("平方根");
JButton mr=new JButton("MR");
JButton four=new JButton("4");
JButton five=new JButton("5");
JButton six=new JButton("6");
JButton mul=new JButton("*");
JButton percent=new JButton("%");
JButton ms=new JButton("MS");
JButton one=new JButton("1");
one.addActionListener(this);
JButton two=new JButton("2");
JButton three=new JButton("3");
JButton reduce=new JButton("-");
JButton inverse=new JButton("1/x");
JButton m=new JButton("M+");
JButton zero=new JButton("0");
JButton fan=new JButton("+/-");
JButton point=new JButton(".");
JButton add=new JButton("+");
JButton result=new JButton("=");
jframe.setLayout(new GridBagLayout());
addC(jframe,toolbar,0,0,6,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,contentPane,0,1,6,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,backspace,0,2,2,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,ce,2,2,2,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,c,4,2,2,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,mc,0,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,seven,1,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,eight,2,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,night,3,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,divsion,4,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,sqrt,5,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,mr,0,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,four,1,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,five,2,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,six,3,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,mul,4,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,percent,5,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,ms,0,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,one,1,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,two,2,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,three,3,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,reduce,4,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,inverse,5,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,m,0,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,zero,1,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,fan,2,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,point,3,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,add,4,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,result,5,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
}
static void addC(Container container,Component component,int gridx,int gridy, int gridwidth,int gridheight,int anchor,int fill,int ipadx,int ipady){
GridBagConstraints gbc=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,1.0,1.0,anchor,fill,insets,ipadx,ipady);
container.add(component,gbc);
}
static Insets insets=new Insets(0,0,0,0);
public void actionPerformed(ActionEvent actionEvent){
String score;
score="1";
textField.setText(score);
}
public static void main(String[] args){
new Ljjsq();
}
}
import java.awt.event.*;
import javax.swing.*;
public class Ljjsq implements ActionListener{
JTextField textField;
public Ljjsq(){
JFrame jframe = new JFrame("LJJSQ");
JToolBar toolbar = new JToolBar();
JButton bianji = new JButton("编辑(E)");
JButton chakan = new JButton("查看(V)");
JButton bangzu = new JButton("帮助(H)");
toolbar.add(bianji);
toolbar.addSeparator();
toolbar.add(chakan);
toolbar.addSeparator();
toolbar.add(bangzu);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(400,250);
jframe.setVisible(true);
toolbar.setFloatable(false);
Container contentPane=new Container();
SpringLayout layout=new SpringLayout();
contentPane.setLayout(layout);
String score="0.0";
textField=new JTextField("sadasd",15);
contentPane.add(textField);
JButton backspace =new JButton("Backspace");
JButton ce=new JButton("CE");
JButton c=new JButton("C");
JButton mc=new JButton("MC");
JButton seven=new JButton("7");
JButton eight=new JButton("8");
JButton night=new JButton("9");
JButton divsion=new JButton("/");
JButton sqrt=new JButton("平方根");
JButton mr=new JButton("MR");
JButton four=new JButton("4");
JButton five=new JButton("5");
JButton six=new JButton("6");
JButton mul=new JButton("*");
JButton percent=new JButton("%");
JButton ms=new JButton("MS");
JButton one=new JButton("1");
one.addActionListener(this);
JButton two=new JButton("2");
JButton three=new JButton("3");
JButton reduce=new JButton("-");
JButton inverse=new JButton("1/x");
JButton m=new JButton("M+");
JButton zero=new JButton("0");
JButton fan=new JButton("+/-");
JButton point=new JButton(".");
JButton add=new JButton("+");
JButton result=new JButton("=");
jframe.setLayout(new GridBagLayout());
addC(jframe,toolbar,0,0,6,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,contentPane,0,1,6,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,backspace,0,2,2,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,ce,2,2,2,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,c,4,2,2,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,mc,0,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,seven,1,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,eight,2,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,night,3,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,divsion,4,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,sqrt,5,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,mr,0,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,four,1,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,five,2,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,six,3,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,mul,4,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,percent,5,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,ms,0,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,one,1,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,two,2,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,three,3,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,reduce,4,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,inverse,5,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,m,0,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,zero,1,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,fan,2,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,point,3,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,add,4,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,result,5,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
}
static void addC(Container container,Component component,int gridx,int gridy, int gridwidth,int gridheight,int anchor,int fill,int ipadx,int ipady){
GridBagConstraints gbc=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,1.0,1.0,anchor,fill,insets,ipadx,ipady);
container.add(component,gbc);
}
static Insets insets=new Insets(0,0,0,0);
public void actionPerformed(ActionEvent actionEvent){
String score;
score="1";
textField.setText(score);
}
public static void main(String[] args){
new Ljjsq();
}
}
----------------解决方案--------------------------------------------------------
程序代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Ljjsq implements ActionListener{
JTextField textField;
public Ljjsq(){
JFrame jframe = new JFrame("LJJSQ");
JToolBar toolbar = new JToolBar();
JButton bianji = new JButton("编辑(E)");
JButton chakan = new JButton("查看(V)");
JButton bangzu = new JButton("帮助(H)");
toolbar.add(bianji);
toolbar.addSeparator();
toolbar.add(chakan);
toolbar.addSeparator();
toolbar.add(bangzu);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(400,250);
toolbar.setFloatable(false);
Container contentPane=new Container();
SpringLayout layout=new SpringLayout();
contentPane.setLayout(layout);
String score="0.0";
textField=new JTextField("sadasd",15);
contentPane.add(textField);
JButton backspace =new JButton("Backspace");
JButton ce=new JButton("CE");
JButton c=new JButton("C");
JButton mc=new JButton("MC");
JButton seven=new JButton("7");
JButton eight=new JButton("8");
JButton night=new JButton("9");
JButton divsion=new JButton("/");
JButton sqrt=new JButton("平方根");
JButton mr=new JButton("MR");
JButton four=new JButton("4");
JButton five=new JButton("5");
JButton six=new JButton("6");
JButton mul=new JButton("*");
JButton percent=new JButton("%");
JButton ms=new JButton("MS");
JButton one=new JButton("1");
one.addActionListener(this);
JButton two=new JButton("2");
JButton three=new JButton("3");
JButton reduce=new JButton("-");
JButton inverse=new JButton("1/x");
JButton m=new JButton("M+");
JButton zero=new JButton("0");
JButton fan=new JButton("+/-");
JButton point=new JButton(".");
JButton add=new JButton("+");
JButton result=new JButton("=");
jframe.setLayout(new GridBagLayout());
addC(jframe,toolbar,0,0,6,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,contentPane,0,1,6,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,backspace,0,2,2,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,ce,2,2,2,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,c,4,2,2,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,mc,0,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,seven,1,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,eight,2,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,night,3,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,divsion,4,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,sqrt,5,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,mr,0,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,four,1,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,five,2,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,six,3,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,mul,4,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,percent,5,4,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,ms,0,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,one,1,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,two,2,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,three,3,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,reduce,4,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,inverse,5,5,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,m,0,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,zero,1,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,fan,2,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,point,3,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,add,4,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
addC(jframe,result,5,6,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,0,0);
jframe.setVisible(true);//控件全部add以后再显示就可以了,如果显示之后再add可能需要重绘面板
}
static void addC(Container container,Component component,int gridx,int gridy, int gridwidth,int gridheight,int anchor,int fill,int ipadx,int ipady){
GridBagConstraints gbc=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,1.0,1.0,anchor,fill,insets,ipadx,ipady);
container.add(component,gbc);
}
static Insets insets=new Insets(0,0,0,0);
public void actionPerformed(ActionEvent actionEvent){
String score;
score="1";
textField.setText(score);
}
public static void main(String[] args){
new Ljjsq();
}
}
----------------解决方案--------------------------------------------------------
学到东西了,谢谢!
----------------解决方案--------------------------------------------------------