2013-12-22 47 views
1

我打算爲最短路徑構建一個GUI。我在for循環中創建了按鈕。現在我想更改按下按鈕的顏色,但是我收到有關Action Listener的錯誤。它說「線程中的異常」主要「java.lang.Error:未解決的編譯問題: 類型AbstractButton中的方法addActionListener(ActionListener)不適用於參數(Final_GUI)」..儘管我已經使用JPanel和它在那裏工作得很好,但是現在它不工作。我想改變按下按鈕的顏色。如果再次按下該按鈕,我希望恢復其以前的顏色。 以下是我想做的事的圖片: enter image description here .. :(不支持鼠標點擊的動作監聽器

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JComboBox; 
import javax.swing.event.*; 
import javax.swing.JFrame; 

public class Final_GUI extends JFrame { 
JLabel label; 
ButtonGroup cbg; 
JRadioButton radio_1; 
JRadioButton radio_2; 
JRadioButton radio_3; 
JCheckBox checkbox_1; 
JCheckBox checkbox_2; 
JCheckBox checkbox_3; 
JScrollPane scrollpane_1; 
JComboBox combobox_1; 
JList list_1; 
JScrollPane sp_list_1; 
JComboBox combobox_2; 
JButton Orange; 
JButton Exit; 
JLabel for_text; 

int check [][]= new int [100][100]; 
int x=100; 
int y=200; 
JFrame frame = new JFrame(); 
    JButton[][] buttons = new JButton[x][y]; 
    JPanel mPanel = new JPanel(); 
    JPanel bPanel = new JPanel(); 
    JPanel cPanel = new JPanel(); 
    JTextArea scoreKeeper = new JTextArea(); 
    Container c = getContentPane(); 
    int[][] intArray = new int[x][y]; 

public Final_GUI() { 

    butGen(); 
     score(); 


    Final_GUILayout customLayout = new Final_GUILayout(); 

    getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12)); 
    getContentPane().setLayout(customLayout); 

    label = new JLabel("Shortest Path Finding Algorithm"); 
    getContentPane().add(label); 

    cbg = new ButtonGroup(); 
    radio_1 = new JRadioButton("radio_1", false); 
    cbg.add(radio_1); 
    getContentPane().add(radio_1); 

    radio_2 = new JRadioButton("radio_2", false); 
    cbg.add(radio_2); 
    getContentPane().add(radio_2); 

    radio_3 = new JRadioButton("radio_3", false); 
    cbg.add(radio_3); 
    getContentPane().add(radio_3); 

    checkbox_1 = new JCheckBox("checkbox_1"); 
    getContentPane().add(checkbox_1); 

    checkbox_2 = new JCheckBox("checkbox_2"); 
    getContentPane().add(checkbox_2); 

    checkbox_3 = new JCheckBox("checkbox_3"); 
    getContentPane().add(checkbox_3); 


    bPanel.setLayout(new GridLayout(x,y)); 

    mPanel.setLayout(new BorderLayout()); 

    mPanel.add(bPanel, BorderLayout.CENTER); 

    scrollpane_1 = new JScrollPane(mPanel); 



    scrollpane_1.setViewportView(mPanel); 
    getContentPane().add(scrollpane_1); 

    combobox_1 = new JComboBox(); 
    combobox_1.addItem("Size1"); 
    combobox_1.addItem("Size2"); 
    getContentPane().add(combobox_1); 

    DefaultListModel listModel_list_1 = new DefaultListModel(); 
    listModel_list_1.addElement("Black"); 
    listModel_list_1.addElement("Green"); 
    list_1 = new JList(listModel_list_1); 
    sp_list_1 = new JScrollPane(list_1); 
    getContentPane().add(sp_list_1); 

    combobox_2 = new JComboBox(); 
    combobox_2.addItem("Additional Data"); 
    combobox_2.addItem("Additional Data2"); 
    getContentPane().add(combobox_2); 

    Orange = new JButton("Orange"); 
    getContentPane().add(Orange); 

    Exit = new JButton("Exit"); 
    getContentPane().add(Exit); 

    for_text = new JLabel("Just For some text"); 
    getContentPane().add(for_text); 

    setSize(getPreferredSize()); 

    addWindowListener(new WindowAdapter() { 
     public void windowClosing(WindowEvent e) { 
      System.exit(0); 
     } 
    }); 
} 

private void butGen() 
{ 
    for(int i=0;i<x;i++) 
     for(int j=0;j<y;j++) 
     { 
      buttons[i][j] = new JButton(String.valueOf(i)+"x"+String.valueOf(j)); 
      buttons[i][j].setActionCommand("button" +i +"_" +j); 
      buttons[i][j].addActionListener(this); 

      buttons[i][j].setBackground(Color.GRAY); 
      buttons[i][j].setSize(100, 100); 
      bPanel.add(buttons[i][j]); 
     } 
} 

public void actionPerformed(ActionEvent e) 
{ 

    if(e.getActionCommand().contains("button")) 
    { 

     String str = e.getActionCommand().replaceAll("button", ""); 
     System.out.println(str); 
     String[] v = str.split("_"); 
     int i = Integer.parseInt(v[0]); 
     int j = Integer.parseInt(v[1]); 

     intArray[i][j]++; 

     if(check[i][j]!=1){ 
      buttons[i][j].setBackground(Color.BLUE); 
      check[i][j]=1; 
     } 
     else{ 
      buttons[i][j].setBackground(null); 
      check[i][j]=0; 
     } 


    System.out.println(e.getActionCommand() +" " +i +" " +j); 

    score(); 
} 
    } 
private void score() 
{ 
    for(int i=0;i<x;i++) 
     for(int j=0;j<y;j++) 
    buttons[i][j].setText(""); 

    } 

public static void main(String args[]) { 
    Final_GUI window = new Final_GUI(); 

    window.setTitle("SHORTEST PATH FINDING ALGORITHM"); 
    window.pack(); 
    window.show(); 
} 
} 

GUILayout

class Final_GUILayout implements LayoutManager { 

public Final_GUILayout() { 
} 

public void addLayoutComponent(String name, Component comp) { 
} 

public void removeLayoutComponent(Component comp) { 
} 

public Dimension preferredLayoutSize(Container parent) { 
    Dimension dim = new Dimension(0, 0); 

    Insets insets = parent.getInsets(); 
    dim.width = 1053 + insets.left + insets.right; 
    dim.height = 621 + insets.top + insets.bottom; 

    return dim; 
} 

public Dimension minimumLayoutSize(Container parent) { 
    Dimension dim = new Dimension(0, 0); 
    return dim; 
} 

public void layoutContainer(Container parent) { 
    Insets insets = parent.getInsets(); 

    Component c; 
    c = parent.getComponent(0); 
    if (c.isVisible()) {c.setBounds(insets.left+368,insets.top+24,304,64);} 
    c = parent.getComponent(1); 
    if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+120,72,24);} 
    c = parent.getComponent(2); 
    if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+144,72,24);} 
    c = parent.getComponent(3); 
    if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+168,72,24);} 
    c = parent.getComponent(4); 
    if (c.isVisible()) {c.setBounds(insets.left+88,insets.top+120,72,24);} 
    c = parent.getComponent(5); 
    if (c.isVisible()) {c.setBounds(insets.left+88,insets.top+144,72,24);} 
    c = parent.getComponent(6); 
    if (c.isVisible()) {c.setBounds(insets.left+88,insets.top+168,72,24);} 
    c = parent.getComponent(7); 
    if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+120,704,488);} 
    c = parent.getComponent(8); 
    if (c.isVisible()) {c.setBounds(insets.left+880,insets.top+120,160,160);} 
    c = parent.getComponent(9); 
    if (c.isVisible()) {c.setBounds(insets.left+24,insets.top+232,128,216);} 
    c = parent.getComponent(10); 
    if (c.isVisible()) {c.setBounds(insets.left+880,insets.top+296,160,216);} 
    c = parent.getComponent(11); 
    if (c.isVisible()) {c.setBounds(insets.left+904,insets.top+528,112,24);} 
    c = parent.getComponent(12); 
    if (c.isVisible()) {c.setBounds(insets.left+888,insets.top+568,144,32);} 
    c = parent.getComponent(13); 
    if (c.isVisible()) {c.setBounds(insets.left+16,insets.top+472,120,48);} 
} 
} 

回答

5

你爲什麼要創建一個自定義佈局管理器是如何佈局的概念你。管理員應該工作是錯誤的,你不應該在你的佈局管理器中使用硬編碼的值,例如,如果你決定改變組件的字體,所有的計算都需要改變,如果你需要添加另一個組件計算將會更改。您的佈局管理器代碼將成爲維護的噩夢。

您應該改用佈局管理器來簡化工作。這可能意味着你需要使用嵌套面板,許多人使用不同的佈局管理器。

說了你所有的使用scrollpane太複雜。您不需要創建水平和垂直滾動條,滾動窗格將爲您管理該滾動條。 ScrollPane的用法應該是這樣的:

JScrollPane scrollPane = new JScrollPane(panel); 

不是:

JScrollPane scrollPane = new JScrollPane(); 
scrollPane.add(panel); 

即面板需要添加到滾動的視,不直接滾動窗格。

+0

謝謝。它現在有效。現在我面臨另一個問題。操作偵聽器不能處理鼠標點擊。它說「在線程中的異常」主要「java.lang.Error:未解決的編譯問題: \t AbstractButton類型中的方法addActionListener(ActionListener)不適用於參數(Final_GUI)」 – waleeds37

+0

我想你Final_GUI類需要實現ActionListener。順便說一句,你在類名中使用「_」?你能否在JDK中向我展示任何一個在類名中使用它的類。按照現有的標準,不要自己做! – camickr

+0

再次感謝您的幫助。我不知道我有時會犯這類愚蠢的錯誤。不管怎樣,謝謝你的建議。從現在開始,我會遵循標準。順便說一句,我通常不會在類名中使用「_」。那只是爲了我自己的方便。沒關係 。祝好運。祝你今天愉快。 :-) – waleeds37