2014-08-31 59 views
1

我一直在試圖讓我的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)); 
    } 

} 
+0

究竟你有什麼期望它看起來像? – 2014-08-31 13:21:47

+0

我希望前三個按鈕位於所有其他按鈕的上方,然後是每行六個按鈕,並且所有這些按鈕直接位於另一個按鈕的下方,而不是像現在這樣按鈕。感謝您的關注 – user3046986 2014-08-31 13:32:51

回答

2

「但我似乎無法讓他們到所有相同的大小,並在對方右側顯示。」

通常這會尖叫出來GridLayout。而不是使用兩個BoxLayout(一個垂直和一個水平),只需使用GridLayout(4, 6)(用於數字按鈕)。請記住,按鈕的大小將是最大按鈕(這是sqrt)的大小。

此外,我建議遠離保持您的面板像你在做一個陣列。很難跟隨你想如何添加一切的流程。給你的面板(和所有組件)的有意義的名稱是自我記錄。

有關佈局管理器的一般知識,請Laying out Component Within a Container

我重構你的代碼可以運行它作爲一個applet或作爲一個獨立的框架。我還包裹在一個面板中的所有的組件和內容窗格的佈局管理器設置爲GridBagLayout,所以一切都被弄擰了,如果你調整框架

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JApplet; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

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 static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() { 
       JFrame frame = new JFrame(); 
       MyCalc calc = new MyCalc(); 
       calc.init(); 
       frame.add(calc); 
       frame.pack(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public void init() { 
     setName("Calculator Applet"); 
     menuBar.add(edit); 
     menuBar.add(view); 
     menuBar.add(help); 
     setJMenuBar(menuBar); 

     Container contentPane = getContentPane(); 

     calc.setText("0."); 
     calc.setHorizontalAlignment(JTextField.RIGHT); 
     JPanel topButtonPanel = new JPanel(new GridLayout(1, 0)); 
     for (int i = 0; i < 3; i++) { 
      topButtonPanel.add(buttons[i]); 
     } 

     JPanel numberButtonPanel = new JPanel(new GridLayout(4, 6)); 
     for (int i = 3; i < 26; i++) { 
      numberButtonPanel.add(buttons[i]); 
     } 

     JPanel centerWrapper = new JPanel(new BorderLayout()); 
     centerWrapper.add(calc, BorderLayout.PAGE_START); 
     centerWrapper.add(topButtonPanel); 
     centerWrapper.add(numberButtonPanel, BorderLayout.PAGE_END); 

     contentPane.setLayout(new GridBagLayout()); 
     contentPane.add(centerWrapper); 
    } 
} 
+0

這的確有竅門,非常感謝! – user3046986 2014-08-31 13:42:20