好吧,試圖總結我的問題。我試圖從一個不同的類添加一個jpanel。在那個類中,我使用print方法從文件中獲取輸入並將其附加到作爲第二個代碼塊的JTextArea。我想將該面板添加到GUI類中的框架。當前代碼正確輸出第一個塊中的按鈕。但點擊選項只會將窗口擴大一點,而且全是黑色的。按鈕保持在那裏,但覆蓋黑色窗口。 =(將面板添加到不同類的框架?
package GamePackage;
import java.*;
import javax.swing.*
public class CopyOfGUI extends JFrame {
static String config = null;
static JFrame frame;
static JPanel panel;
private static final long serialVersionUID = 1L;
public CopyOfGUI() {
frame = new JFrame("Sheep City");
panel = new JPanel();
JButton options = new JButton("OPTIONS");
JButton start = new JButton ("START");
JButton controls = new JButton ("CONTROLS");
panel.add(options);
panel.add(start);
panel.add(controls);
frame.add(panel);
options.addActionListener(new actionReadConfig(config));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args) {
if (args.length > 0)
{config = args[0];}
CopyOfGUI gui = new CopyOfGUI();
}
//Here is where the options button loads the read in argument for the config file
class actionReadConfig implements ActionListener {
String config = null;
public actionReadConfig(String config) {
this.config = config;
}
public void actionPerformed(ActionEvent e) {
try {
GameBoard.loadConfig(this.config);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
這裏是即時試圖添加從讀取到的文本區域的文件中的文本,將它添加到面板中的代碼,並返回到原來的幀。
public static int print(ConfigurationManager cm) {
//code gere
JPanel configPanel = new JPanel(new BorderLayout());
configPanel.setBorder(BorderFactory.createTitledBorder("Configuration Manager"));
JTextArea area = new JTextArea();
area.append("Before we get started lets setup our configurations shall we?\n");
area.append("These are the default configurations.\n");
area.append("*************************\n");
//for loop here {
//area.append output
//}
area.append("*************************\n");
area.append("Would you like to change any of these values? (yes/no)\n");
area.setEditable(false);
configPanel.add(area, BorderLayout.SOUTH); //Idk if any of this is right
CopyOfGUI.frame.add(configPanel);
CopyOfGUI.frame.remove(CopyOfGUI.panel);
CopyOfGUI.frame.pack();
CopyOfGUI.frame.setVisible(true);
CopyOfGUI.frame.repaint();
更多的代碼之後,但我需要的前面部分的工作之前,我擔心休息。Thx提前。
只是一個觀察 - 這是相當困難的人來幫助你,當你寫洙多,你的代碼仍然是不完整的(例如誰在打印方法?)。儘量簡潔,將問題降到最低限度,並提供一些可編譯的代碼。見http://sscce.org/ – aditsu
@aditsu謝謝我試圖總結一下,但我認爲這就像我可以做的代碼 – waaffles