2016-02-15 51 views
0

我已經通過本網站和通過Java網站搜索得很遠,試圖找出答案,但我找不到具體到我的問題的答案。無法格式化GUI /添加動作偵聽器

我不知道把我的動作監聽器放在b1b2的哪個位置,我也認爲我正在使用的方法可能有問題(儘管代碼仍然編譯並運行,並且沒有任何操作監聽器。)

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class CubeCalc { 

    public static void MakeTitlePage() 
    { 
     JButton b1 = new JButton("Start"); 
     b1.setBackground(Color.decode("#5A20DF")); 
     b1.setForeground(Color.WHITE); 
     b1.setLayout(new GridBagLayout()); 
     b1.setPreferredSize(new Dimension(150,50)); 

     JButton b2 = new JButton("Information about the Developer"); 
     b2.setBackground(Color.decode("#23D123")); 
     b2.setForeground(Color.BLACK); 
     b2.setLayout(new GridBagLayout()); 
     b2.setPreferredSize(new Dimension(275,50)); 

     GridBagConstraints blo = new GridBagConstraints(); 
     blo.fill = GridBagConstraints.HORIZONTAL; 
     blo.gridx = 0; 
     blo.gridy = 1; 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// 
     JPanel start = new JPanel(new GridBagLayout()); 
     start.setBackground(Color.BLACK); 
     start.setPreferredSize(new Dimension(300,100)); 
     start.add(b1, blo); 

     JPanel info = new JPanel(new GridBagLayout()); 
     info.setBackground(Color.BLACK); 
     info.setPreferredSize(new Dimension(300,100)); 
     info.add(b2, blo); 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// 
     JFrame window = new JFrame("Cubic Feet Calculator"); //Creates Frame 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      /* window.add(new JLabel(new ImageIcon("Harold.jpg")));*/ 
       window.add(start, BorderLayout.NORTH); 
       window.add(info, BorderLayout.SOUTH); 

     window.pack(); //resizes to minimum possible frame size 
     //window.setSize(500,500); //Sets size of frame 
     window.setLocationRelativeTo(null); 
     window.setVisible(true); //Sets the frame to be visible 
     window.setResizable(true); 
     window.setBackground(Color.BLACK); 

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// 
    } 

    public static void main(String[] args) { 
     MakeTitlePage(); 
    } 
} 

我應該在哪裏加我ActionListener,是有什麼,我應該用我的方法做什麼?

+0

好吧,按鈕引用的MakeTitlePage方法的局部變量,因此從這個方法才能訪問,所以唯一的地方在哪裏在該方法中添加動作偵聽器。請注意,按照慣例,Java方法應始終以小寫字母開頭。 –

回答

3

評論:

  • 的按鈕設置佈局沒有必要
  • 使用添加ActionListener到按鈕addActionListener
  • 使用Event Dispatch Thread發揮出色效果,使用從main啓動幀3210
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class CubeCalc { 
    public static void MakeTitlePage() 
    { 
     final JFrame window = new JFrame("Cubic Feet Calculator"); // Creates Frame (created first so the button can reference it for JOptionPane 

     JButton b1 = new JButton("Start"); 
     b1.setBackground(Color.decode("#5A20DF")); 
     b1.setForeground(Color.WHITE); 
     //b1.setLayout(new GridBagLayout()); << unnecessary 
     b1.setPreferredSize(new Dimension(150,50)); 
     b1.addActionListener(new ActionListener() { // action when button is pressed 
      int pressCount=0; 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       switch(++pressCount) { 
        case 1: JOptionPane.showMessageDialog(window, "Hey, stop pressing me!"); break; 
        case 2: JOptionPane.showMessageDialog(window, "I said, stop pressing me!!!!"); break; 
        default: JOptionPane.showMessageDialog(window, "Aaaaaaaaargl!!!!"); break; 
       } 
      } 
     }); 

     JButton b2 = new JButton("Information about the Developer"); 
     b2.setBackground(Color.decode("#23D123")); 
     b2.setForeground(Color.BLACK); 
     //b2.setLayout(new GridBagLayout()); << unnecessary 
     b2.setPreferredSize(new Dimension(275,50)); 
     b2.addActionListener(new ActionListener() { // action when button is pressed 
      int pressCount=0; 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       switch(++pressCount) { 
        case 1: JOptionPane.showMessageDialog(window, "There is no information here!"); break; 
        case 2: JOptionPane.showMessageDialog(window, "Stop asking me for information!!!!"); break; 
        default: JOptionPane.showMessageDialog(window, "Aaaaaaaaargl!!!!"); break; 
       } 
      } 
     }); 

     GridBagConstraints blo = new GridBagConstraints(); 
     blo.fill = GridBagConstraints.HORIZONTAL; 
     blo.gridx = 0; 
     blo.gridy = 1; 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// 
     JPanel start = new JPanel(new GridBagLayout()); 
     start.setBackground(Color.BLACK); 
     start.setPreferredSize(new Dimension(300,100)); 
     start.add(b1, blo); 

     JPanel info = new JPanel(new GridBagLayout()); 
     info.setBackground(Color.BLACK); 
     info.setPreferredSize(new Dimension(300,100)); 
     info.add(b2, blo); 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     /* window.add(new JLabel(new ImageIcon("Harold.jpg")));*/ 
     window.add(start, BorderLayout.NORTH); 
     window.add(info, BorderLayout.SOUTH); 

     window.setLocationRelativeTo(null); 
     window.setResizable(true); 
     window.setBackground(Color.BLACK); 
     window.pack(); //resizes to minimum possible frame size 
     window.setVisible(true); //Sets the frame to be visible 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { // launch frame on the Event Dispatch Thread 
      @Override 
      public void run() { 
       MakeTitlePage(); 
      } 
     }); 
    } 
} 

效果:

enter image description here

+0

謝謝!將「final」添加到聲明JFrame的部分有什麼區別? – Buttchinbro

+0

@Buttchinbro要引用封閉範圍中的變量,該變量需要聲明爲「final」。試着刪除'final',你會看到一個編譯錯誤。 –

+0

以及如果我想讓'b1'按下'b1'打開一個新頁面並關閉原始標題頁,我會從事件派發線程還是在動作偵聽器中啓動新的GUI? – Buttchinbro

1

您可以嘗試是這樣的:在片斷

b1.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) 
      { 
       // what do you want to execute... 

      } 
     });