我知道之前发布了很多类似的问题,但没有一个能帮助我。
我创建了一个JFrame
,其中包含多个JPanel
- s,其中包括一些内部有JTable
个内容的JFrame
。但我不能使它适合pack()
。我试过JScrollPane
;这不起作用,我试图添加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);
}
}
,但也没有解决问题。我刚开始使用GUI,所以布局选项有点难以弄清楚,也许这可能是问题?
以下是代码:
{{1}}
答案 0 :(得分:1)
在actionPerformed
方法中添加
this.pack();
在方法结束时。
这将使所有内容都显示,但您可能需要考虑使用面板中的布局管理器来调整外观,即布局。