0
我曾與絕對定位(的setBounds和空佈局) 現在開始練佈局管理器, 這個代碼與網格包佈局,但不被顯示很少的部件,無論是有一些問題,與細胞或別的東西 請幫忙!GridBagLayout中不能正常工作
import java.util.StringTokenizer; import java.awt.event.*; import java.awt.*; import javax.swing.*; class Calculator extends JFrame { JButton add,sub,mul,div,sin,cos,tan,clear,negate,inverse,zero,one,two,three,four,five,six,seven,eight,nine,equalTo,percentage,sqrt; JTextField input; GridBagLayout gbl; private void addComponent(Component component,int gridx, int gridy, int gridwidth, int gridheight,Insets insets) { add(component, new GridBagConstraints(gridx, gridy,gridwidth, gridheight, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0,0)); } Calculator() { //setSize(500,400); setVisible(true); setLayout(gbl=new GridBagLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add= new JButton("+"); sub= new JButton("-"); mul= new JButton("*"); div= new JButton("/"); sin= new JButton("sin"); cos= new JButton("cos"); tan= new JButton("tan"); clear= new JButton("C"); negate= new JButton("neg"); inverse= new JButton("1/x"); zero= new JButton("0"); one= new JButton("1"); two= new JButton("2"); three= new JButton("3"); four= new JButton("4"); five= new JButton("5"); six= new JButton("6"); seven= new JButton("7"); eight= new JButton("8"); nine= new JButton("9"); equalTo= new JButton("="); percentage= new JButton("%"); sqrt= new JButton("sqrt"); input = new JTextField(20); addComponent(input,0,0,0,1,new Insets(10,10,100,4)); //tldr addComponent(add,0,1,1,1,new Insets(4,4,4,4)); addComponent(sub,1,1,2,1,new Insets(4,4,4,4)); addComponent(mul,2,1,3,1,new Insets(4,4,4,4)); // this is not displayed addComponent(div,3,1,4,1,new Insets(4,4,4,4)); addComponent(sin,0,2,1,2,new Insets(4,4,4,4)); addComponent(cos,1,2,2,2,new Insets(4,4,4,4)); addComponent(tan,2,2,3,2,new Insets(4,4,4,4)); // this is not displayed addComponent(clear,3,2,4,2,new Insets(4,4,4,4)); addComponent(negate,0,3,1,3,new Insets(4,4,4,4)); // these 4 are not visible addComponent(inverse,1,3,2,3,new Insets(4,4,4,4)); addComponent(zero,2,3,3,3,new Insets(4,4,4,4)); addComponent(one,3,3,4,3,new Insets(4,4,4,4)); pack(); } public static void main(String...args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new Calculator(); } }); } }
我正在採取gridwidth,gridheight絕對的,而不是PO sitional(相對)這是錯誤的 – Gagan93