我知道之前有很多类似的问题,但没有一个对我有帮助。
我创建了一个JFrame
,其中包含几个JPanel
-s,这里包括一些内部有几个JTable
-s。但我无法使其适合 JFrame
。我已经尝试过 pack()
;这不起作用,我尝试添加 JScrollPane
,但这也不能解决问题。我刚开始使用 GUI,因此布局选项有点难以弄清楚,也许这可能是问题所在?
这是代码:
public class Frame extends JFrame implements ActionListener{
private JPanel mainScreen, menu1, menu2, menu3;
private JButton defTeamsButton, userTeamsButton, menu1Button1, menu2Button1, menu1backButton, menu2backButton,menu3backButton;
private JTable table1, table2, table3;
private JScrollPane scrollPane, scrollPane2, scrollPane3;
private JScrollBar bar;
private JTextField jtfDay;
private JLabel jlblDay;
public Frame(){
setLayout(new GridLayout(2,0));
mainScreen = new JPanel();
defTeamsButton = new JButton("Default/ preset teams");
defTeamsButton.setLocation(0, 20);
defTeamsButton.setSize(250, 30);
userTeamsButton = new JButton("Add my own teams");
userTeamsButton.setLocation(0, 60);
userTeamsButton.setSize(250, 30);
menu1 = new JPanel();
menu1Button1 = new JButton("Display fixture table");
menu1Button1.setLocation(0, 20);
menu1Button1.setSize(250, 30);
menu1backButton = new JButton("Back");
menu2Button1 = new JButton("Display standings table");
menu2Button1.setLocation(0, 40);
menu2Button1.setSize(250, 30);
menu2 = new JPanel();
table1 = new JTable(new tables());
scrollPane = new JScrollPane(table1);
scrollPane.setBorder (BorderFactory.createTitledBorder (BorderFactory.createEtchedBorder (),
"Sunday 1",
TitledBorder.CENTER,
TitledBorder.TOP));
table2 = new JTable(new tables2());
table2.setSize(300, 200);
scrollPane2 = new JScrollPane(table2);
scrollPane2.setBorder (BorderFactory.createTitledBorder (BorderFactory.createEtchedBorder (),
"Wednesday 1",
TitledBorder.CENTER,
TitledBorder.TOP));
menu2backButton = new JButton("Back");
menu3 = new JPanel();
table3 = new JTable(new tables3());
scrollPane3 = new JScrollPane(table3);
menu3backButton = new JButton("Back");
mainScreen.add(defTeamsButton);
mainScreen.add(userTeamsButton);
menu1.add(menu1Button1);
menu1.add(menu2Button1);
menu1.add(menu1backButton);
menu2.add(menu2backButton);
menu2.add(scrollPane);
menu2.add(scrollPane2);
menu3.add(scrollPane3);
menu3.add(menu3backButton);
add(mainScreen);
add(mainScreen);
//register listener with buttons
defTeamsButton.addActionListener(this);
userTeamsButton.addActionListener(this);
menu1Button1.addActionListener(this);
menu2Button1.addActionListener(this);
menu1backButton.addActionListener(this);
menu2backButton.addActionListener(this);
menu3backButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource() == defTeamsButton){
//define action for defTeamsButton
getContentPane().removeAll();
getContentPane().add(menu1);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
else if(e.getSource() == userTeamsButton)
{
//ask user to input teams, to do..
String Team1= "";
String Team2= "";
String Team3= "";
String Team4= "";
Team1 = JOptionPane.showInputDialog(
null, "Please enter your First Team: ");
Team2 = JOptionPane.showInputDialog(
null, "Please enter your First Team: ");
Team3 = JOptionPane.showInputDialog(
null, "Please enter your First Team: ");
Team4 = JOptionPane.showInputDialog(
null, "Please enter your First Team: ");
JOptionPane.showMessageDialog (null," your teams are" + Team1 + Team2
+ Team3 + Team4);
}
else if(e.getSource() == menu1Button1){
getContentPane().removeAll();
getContentPane().add(menu2);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
else if(e.getSource() == menu2Button1)
{
getContentPane().removeAll();
getContentPane().add(menu3);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
else if(e.getSource() == menu1backButton)
{
getContentPane().removeAll();
getContentPane().add(mainScreen);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
else if(e.getSource() == menu2backButton)
{
getContentPane().removeAll();
getContentPane().add(menu1);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
}
public class tables extends AbstractTableModel {
/**
*
*/
private String[] columnNames = {"Home",
"Away",};
private String[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
public class tables2 extends AbstractTableModel {
/**
*
*/
private String[] columnNames = {"Home",
"Away",
};
private String[][] data = {
{"Team A", "Team B"},
{"Team C", "Team D"},
{"Team E", "Team F"},
{"Team G", "Team H"},
{"Team I", "Team H"}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
public class tables3 extends AbstractTableModel {
/**
*
*/
private String[] columnNames = {"Teams",
"Goals",
"GoalDifference",
"Score"
};
private Object[][] data = {
{"Kathy", new Integer(5), new Integer(5), new Integer(5)},
{"John", new Integer(5), new Integer(4), new Integer(5)},
{"Sue", new Integer(5), new Integer(3), new Integer(5)},
{"Jane", new Integer(5), new Integer(2), new Integer(5)},
{"Joe", new Integer(5), new Integer(1), new Integer(5)}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
public static void main(String args[]){
JFrame frame = new Frame();
frame.setTitle("Choose an option");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}
最佳答案
在您的 actionPerformed
方法中添加
this.pack();
在方法的末尾。
这将使所有内容显示出来,但您可能需要考虑在面板中使用布局管理器来调整外观,即布局。
关于java - 使多个 JPanel 及其 JTable 适合 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20939553/