我一直在試圖讓我的JApplet以有序的方式顯示計算器applet所需的所有按鈕,但我似乎無法讓他們都是相同的大小,並顯示在對方下。我應該使用不同的佈局還是更改整個設計?提前致謝。無法獲得計算器JApplet GUI以有序方式顯示按鈕
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyCalc extends JApplet implements ActionListener{
/**
*
*/
protected static final long serialVersionUID = 3169756252830354073L;
private JMenuBar menuBar = new JMenuBar();
private JMenu edit = new JMenu("Edit");
private JMenu view = new JMenu("View");
private JMenu help = new JMenu("Help");
//Instantiated all JPanels used in this Applet
private JPanel[] rows = {new JPanel(), new JPanel(), new JPanel(),
new JPanel(), new JPanel(), new JPanel(), new JPanel()};
JTextField calc = new JTextField(1);
private JButton[] buttons = {new JButton("Backspace"), new JButton("CE"), new JButton("C"),
new JButton("MC"), new JButton("7"), new JButton("8"), new JButton("9"),
new JButton("/"), new JButton("sqrt"), new JButton("MR"), new JButton("4"),
new JButton("5"), new JButton("6"), new JButton("*"), new JButton("%"),
new JButton("MS"), new JButton("1"), new JButton("2"), new JButton("3"),
new JButton("-"), new JButton("1/x"), new JButton("M+"), new JButton("0"),
new JButton("+/-"), new JButton("."), new JButton("+"), new JButton("=")};
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttons[0])
{
}
}
public void init()
{
setName("Calculator Applet");
menuBar.add(edit);
menuBar.add(view);
menuBar.add(help);
setJMenuBar(menuBar);
rows[0].setLayout(new BoxLayout(rows[0], BoxLayout.Y_AXIS));
getContentPane();
add(rows[0]);
setSize(300, 200);
calc.setText("0.");
calc.setHorizontalAlignment(JTextField.RIGHT);
for(int i = 0; i < 3; i++)
{
rows[2].add(buttons[i]);
}
for(int i = 3; i < 9; i++)
{
rows[3].add(buttons[i]);
}
for(int i = 9; i < 15; i++)
{
rows[4].add(buttons[i]);
}
for(int i = 15; i < 21; i++)
{
rows[5].add(buttons[i]);
}
for(int i = 21; i < 26; i++)
{
rows[6].add(buttons[i]);
}
rows[1].add(calc);
rows[1].setLayout(new GridLayout());
rows[2].setLayout(new GridLayout());
rows[0].add(rows[1]);
rows[0].add(rows[2]);
for(int i = 3; i < 7; i++)
{
rows[0].add(rows[i]);
rows[i].setLayout(new FlowLayout(FlowLayout.CENTER));
}
}
究竟你有什麼期望它看起來像? – 2014-08-31 13:21:47
我希望前三個按鈕位於所有其他按鈕的上方,然後是每行六個按鈕,並且所有這些按鈕直接位於另一個按鈕的下方,而不是像現在這樣按鈕。感謝您的關注 – user3046986 2014-08-31 13:32:51