我一直試圖在我的代碼中遵循一個MVC模式,並且我正在創建一個系統,在該模型中我有一個JTabbedPane和Jbuttons,這個JTabbedPane在我的View中的面板內,模型中的內容信息,和我的控制器上的actionlisteners。 我一直有的問題是,我一直試圖把一個actionlistener放在我的Jbuttons上,但它似乎只在我的最後一個選項卡上工作(比方說,我有4個選項卡,只有第4個選項卡與jbuttons工作) 哦,我應該說我一直在使用Jbutton陣列 請注意,我一直只使用一個Jbutton陣列放置在我的面板上。 該如何工作?我需要一個示例代碼以供參考。JTabbedPane上的Java MVC模式
這裏是我的樣品View類
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class View extends JFrame
{
private static final int HEIGHT = 700;
private static final int WIDTH = 700;
private JTabbedPane tabPane = new JTabbedPane();
private JComponent tab1 = makePanel("1");
private JComponent tab2 = makePanel("2");
private JComponent tab3 = makePanel("3");
private JComponent tab4 = makePanel("4");
private JButton[] button;
private String[] buttonlabel;
public View()
{
setLayout(null);
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
setResizable(false);
setVisible(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
tabPane.addTab("firstTab", tab1);
tabPane.addTab("secondTab", tab2);
tabPane.addTab("thirdTab", tab3);
tabPane.addTab("fourtTab", tab4);
add(tabPane);
tabPane.setBounds(20, 20, 400, 400);
}
protected JComponent makePanel(String input)
{
JPanel panel = new JPanel(false);
panel.setLayout(null);
if(input == "1")
{
button = new JButton[4];
buttonlabel = new String[]
{
"1button1",
"2button1",
"3button1",
"4button1"
};
for(int x = 0; x < 4; x++)
{
button[x] = new JButton(buttonlabel[x]);
panel.add(button[x]);
}
int yCord = 20;
for(int x = 0; x < 4; x += 2)
{
int xCord = 20;
for(int y = x; y < (x + 2); y++)
{
button[y].setBounds(xCord, yCord, 100, 100);
xCord += 120;
}
yCord += 120;
}
}
else if(input == "2")
{
button = new JButton[4];
buttonlabel = new String[]
{
"1button2",
"2button2",
"3button2",
"4button2"
};
for(int x = 0; x < 4; x++)
{
button[x] = new JButton(buttonlabel[x]);
panel.add(button[x]);
}
int yCord = 20;
for(int x = 0; x < 4; x += 2)
{
int xCord = 20;
for(int y = x; y < (x + 2) && y < 5; y++)
{
button[y].setBounds(xCord, yCord, 100, 100);
xCord += 120;
}
yCord += 120;
}
}
else if(input == "3")
{
button = new JButton[4];
buttonlabel = new String[]
{
"1button3",
"2button3",
"3button3",
"4button3"
};
for(int x = 0; x < 4; x++)
{
button[x] = new JButton(buttonlabel[x]);
panel.add(button[x]);
}
int yCord = 20;
for(int x = 0; x < 4; x += 2)
{
int xCord = 20;
for(int y = x; y < (x + 2) && y < 5; y++)
{
button[y].setBounds(xCord, yCord, 100, 100);
xCord += 120;
}
yCord += 120;
}
}
else if(input == "4")
{
button = new JButton[4];
buttonlabel = new String[]
{
"1button4",
"2button4",
"3button4",
"4button4"
};
for(int x = 0; x < 4; x++)
{
button[x] = new JButton(buttonlabel[x]);
panel.add(button[x]);
}
int yCord = 20;
for(int x = 0; x < 4; x += 2)
{
int xCord = 20;
for(int y = x; y < (x + 2) && y < 5; y++)
{
button[y].setBounds(xCord, yCord, 100, 100);
xCord += 120;
}
yCord += 120;
}
}
return panel;
}
public void buttonListener(ActionListener buttonL)
{
for(int x = 0; x < button.length; x++)
{
button[x].addActionListener(buttonL);
}
}
public static void main(String args[])
{
View view = new View();
Controller control = new Controller(view);
view.setVisible(true);
}
}
這裏是我的樣品Controller類
如果不是不可能的話,你的問題將會很難回答,因爲它基本上是:「請猜測爲什麼我沒有顯示你的代碼不工作」。請通過創建一個新的編譯,運行的小程序來改善這個問題,這個程序足夠小,可以在代碼格式的文本中發佈您的問題,並且可以證明您的問題,[mcve]。 –
我很抱歉沒有發佈代碼,因爲我正在創建示例類。我編輯了我的問題併發布了一個示例代碼。 –
感謝您發佈代碼,我會立即查看它,但是一個顯而易見的問題是:else if(input ==「2」)'。不要使用'=='或'!='比較字符串。改爲使用「equals(...)」或「equalsIgnoreCase(...)」方法。理解'=='檢查兩個*對象引用*是否相同,而不是你感興趣的。另一方面,方法檢查兩個字符串是否具有相同順序的相同字符,這就是這裏很重要。 –