1
當我把我的JPanel用BorderLayout的工作,它告訴我:constructer JPanel in class JPanel cannot be applied to given types; required: no arguments; found: BorderLayout; reason: actual and formal argument lists differ in length;
爲什麼我的BorderLayout不能與我的JPanel一起工作?
這裏是我的代碼:
public class MTGSAMPServerReference extends JFrame implements ActionListener {
public static Toolkit tk = Toolkit.getDefaultToolkit();
static int ScrnWidth = ((int) tk.getScreenSize().getWidth());
static int ScrnHeight = ((int) tk.getScreenSize().getHeight());
private static final long serialVersionUID = 1L;
private static JList list1;
private static JButton next;
public MTGSAMPServerReference() {
// set flow layout for the frame
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
Object[] data1 = { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" };
JPanel controls = new JPanel(new BorderLayout(5,5)); // The line getting the main error.
list1 = new JList<Object>(data1);
list1.setVisibleRowCount(5);
next = new JButton("Next");
next.addActionListener(this);
controls.add(new JScrollPane(list1)); // A result error of the JPanel error^
controls.add(next, BorderLayout.PAGE_END); // A result error of the JPanel error^
// adjust numbers as needed.
controls.setBorder(new EmptyBorder(25,25,0,0));
add(controls); // A result error of the JPanel error^
// add list to frame
add(list1);
add(next);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Next")) {
int index = list1.getSelectedIndex();
System.out.println("Index Selected: " + index);
String s = (String) list1.getSelectedValue();
System.out.println("Value Selected: " + s);
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame f = new MTGSAMPServerReference();
//Display the window.
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1200, 800);
f.setLocationRelativeTo(null);
list1.setSize(250, 250);
list1.setLocation(0, 0);
next.setSize(75, 25);
next.setLocation(251, 276);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
缺什麼在我的代碼? 我有我的所有進口,我相信這只是一個小錯誤,也許是一個錯字。
任何和所有幫助表示讚賞!
在此先感謝!
編輯:這裏是我有進口:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
爲什麼你忽略了'import'語句? –
@AndrewThompson人們總是告訴我只輸入必要的代碼。過去我被告知不包括進口產品。我應該包括他們嗎? – knorberg
*「我應該嗎?」*我會把「包裝進口」放入SSCCE,就像我爲我提供的例子一樣。它僅向源代碼添加了5行(在這種情況下),並且非常清楚哪些軟件包包含這些類。順便說一句,當我**做**添加在SSCCE中看到的進口 - 代碼爲我編譯,沒有任何警告或錯誤。 –