我使用带有单个JFrame和多个JPanel的borderlayout。我的第二个JPanel窗口很小,我的第三个JPanel根本没有显示,我只是得到一个灰色的窗口。真的很感激,如果有人能告诉我什么是错的。以下是我的四个班级。在此先感谢:)
import javax.swing.Box;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.sql.*;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class FrontScreen implements ActionListener
{
private static JRadioButton rb1, rb2, rb3;
private static final JFrame f = new JFrame("Welcome to Ballon d'or , created by Darren Estcourt");
private static JButton b;
private static JPanel myFirstPanel;
private static JLabel ballondor , trophylabel;
private static String firstName , surname;
public FrontScreen()
{
}
public void CreateAndShowFrontScreen()
{
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
myFirstPanel = new JPanel();
myFirstPanel.setBackground(Color.GREEN);
f.add(myFirstPanel, BorderLayout.CENTER);
ballondor = new JLabel("Ballon d'or");
ballondor.setToolTipText("Ballon d'or tooltip");
myFirstPanel.add(ballondor);
Icon trophy = new ImageIcon(getClass().getResource("Ballondor.jpg"));
trophylabel = new JLabel("Please choose an option, then click OK" , trophy, SwingConstants.RIGHT);
trophylabel.setToolTipText("Trophy Label tooltip");
myFirstPanel.add(trophylabel);
rb1 = new JRadioButton("Start New Game");
rb2 = new JRadioButton("Load Game");
rb3 = new JRadioButton("Quit");
Box box1 = Box.createVerticalBox();
box1.add(rb1); // TEST VERTICAL RADIO BUTTONS !
box1.add(rb2);
box1.add(rb3);
myFirstPanel.add(box1);
ButtonGroup bg=new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
b = new JButton("OK");
myFirstPanel.add(b);
b.addActionListener(this);
f.addWindowListener(new WindowAdapter()
{
@Override public void windowClosing(WindowEvent e)
{
if(JOptionPane.showConfirmDialog(f, "Are you sure ?" , "Warning", JOptionPane.YES_NO_OPTION ) == JOptionPane.YES_OPTION)
{
f.setVisible(false);
f.dispose();
}
else{
}
}
});
} // OUT OF SCOPE !!!!!!
@Override public void actionPerformed(ActionEvent e)
{
if(rb1.isSelected())
{
myFirstPanel.setVisible(false);
ManagerName panel = new ManagerName();
f.add(panel);
f.pack();
panel.setVisible(true);
}
if(rb2.isSelected())
{
JOptionPane.showMessageDialog(f ,"Which saved game would you like to load?", "Load game", JOptionPane.QUESTION_MESSAGE);
}
if(rb3.isSelected())
{
System.exit(0);
}
}
public static void main(String[] args) throws SQLException , ClassNotFoundException, InstantiationException, IllegalAccessException
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
FrontScreen test = new FrontScreen();
test.CreateAndShowFrontScreen();
f.pack();
f.setVisible(true);
}
});
Database passFirstNameObject = new Database(firstName , surname);
passFirstNameObject.setFirstName(firstName);
} // end main
}
------------------------------------------------------------------------------
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.sql.*;
/**
*
* @author Darren Estcourt
*/
public class ManagerName extends JPanel implements ActionListener
{
private final JLabel firstNameLabel , surnameLabel;
private final JButton confirmNames , quit;
private String firstName , surname;
private final JTextField myTextField , myTextField2;
public ManagerName()
{
setBackground(Color.GREEN);
firstNameLabel = new JLabel("Please enter your first name");
add(firstNameLabel);
myTextField = new JTextField(20); // or use 20 columns
add(myTextField);
myTextField.addActionListener(this);
surnameLabel = new JLabel("Please enter your surname");
add(surnameLabel);
myTextField2 = new JTextField(20);
add(myTextField2);
myTextField2.addActionListener(this);
confirmNames = new JButton("Submit Names");
confirmNames.addActionListener(this);
add(confirmNames);
quit = new JButton("Quit");
quit.addActionListener(this);
add(quit);
}
@Override public void actionPerformed(ActionEvent e)
{
if(e.getSource()==confirmNames)
{
firstName = myTextField.getText();
surname = myTextField2.getText();
try{
Database passFirstNameObject = new Database(firstName , surname);
passFirstNameObject.setFirstName(firstName);
passFirstNameObject.setSurname(surname);
Database.databaseMethod(passFirstNameObject);
}
catch(SQLException i)
{
System.err.println("Got an exception! ");
}
catch(ClassNotFoundException a)
{
System.err.println("Got an exception! ");
}
catch(InstantiationException b)
{
System.err.println("Got an exception! ");
}
catch(IllegalAccessException c)
{
System.err.println("Got an exception! ");
}
JOptionPane.showMessageDialog(null, "Your name is : " + firstName + " " + surname);
this.setVisible(false);
NewGame callNewGamePanel = new NewGame();
this.add(callNewGamePanel);
callNewGamePanel.setVisible(true);
}
if(e.getSource()==quit)
{
System.exit(0);
}
}
}
------------------------------------------------------------------------
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import java.awt.Color;
public class NewGame extends JPanel implements ActionListener
{
private final String[] premierLeagueClubs = {"Arsenal" , "Bournemouth" , "Burnley" , "Chelsea" , "Crystal Palace" ,
"Everton" , "Hull City" , "Leicester City" , "Liverpool" , "Manchester United" , "Manchester City" , "Middlesborough" ,
"Southampton" , "Stoke City", "Sunderland", "Swansea City", "Tottenham Hotspur" , "Watford" , "West Brom" , "West Ham"};
private final JRadioButton[] rb = new JRadioButton[20];
JButton confirmTeam , quit;
String teamName;
JLabel label1;
public NewGame()
{
setBackground(Color.GREEN);
label1 = new JLabel("Please choose a team");
add(label1);
for (int i = 0; i < premierLeagueClubs.length; i++)
{
rb[i] = new JRadioButton(premierLeagueClubs[i]);
rb[i].setActionCommand(premierLeagueClubs[i]);
}
confirmTeam = new JButton("OK");
confirmTeam.addActionListener(this);
add(confirmTeam);
for(int j=0; j < 20; j++)
{
add(rb[j]);
}
ButtonGroup bg=new ButtonGroup();
int startvalueBG;
int endvalueBG=19;
for(startvalueBG=0; startvalueBG <= endvalueBG; startvalueBG++)
{
bg.add(rb[startvalueBG]);
}
quit = new JButton("Quit");
quit.addActionListener(this);
add(quit);
}
@Override public void actionPerformed(ActionEvent e)
{
if(e.getSource()==confirmTeam)
{
for(int i=0; i < 20; i++){
if(rb[i].isSelected())
{
JOptionPane.showMessageDialog(null , "You chose:" + premierLeagueClubs[i]);
}
}
if(e.getSource()==quit)
{
System.exit(0);
}
}
}
}
------------------------------------------------------------------------------
import java.sql.*;
public class Database
{
private String firstName , surname;
public Database(String firstName , String surname)
{
this.firstName = firstName;
this.surname = surname;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getFirstName()
{
return firstName;
}
public void setSurname(String surname)
{
this.surname = surname;
}
public String getSurname()
{
return surname;
}
public static void databaseMethod(Database passFirstNameObject) throws SQLException , ClassNotFoundException, InstantiationException,IllegalAccessException
{
String myDriver = "com.mysql.jdbc.Driver";
String myUrl = "jdbc:mysql://localhost:3306/ballondor?autoReconnect=true&useSSL=false";
Connection connOne = DriverManager.getConnection(myUrl, "root", "Lucia290907");
Connection connTwo = DriverManager.getConnection(myUrl, "root", "Lucia290907");
Statement st1 = connOne.createStatement();
Statement st2 = connTwo.createStatement();
Class.forName(myDriver).newInstance();
JOptionPane.showMessageDialog(null,"List of team names" ,TeamName, JOptionPane.QUESTION_MESSAGE);
System.out.println("The first attempt :" + passFirstNameObject.getFirstName() + passFirstNameObject.getSurname());
st2.executeUpdate("insert into managername (firstName,surname) values('"+passFirstNameObject.getFirstName()+"' , '"+passFirstNameObject.getSurname()+"')"); // UPDATING MYSQL DATABASE
// } // end while loop
st1.close();
}
}
答案 0 :(得分:2)
您的第二个窗口很小,因为您调用!Feature[1]!
,这将调整窗口大小以符合其子组件的首选大小:
来自pack()
:
FrontScreen
您的第三个面板( ManagerName panel = new ManagerName();
f.add(panel);
f.pack();
)从未显示,因为您首先使第二个面板(NewGame
)不可见,然后将第三个面板添加到其中,因此显然它也是不可见的。
请注意,如果子组件的容器不可见,则在子组件上调用ManagerName
将不会突然显示它。
来自setVisible(true)
(第二个小组):
ManagerName
致电
this.setVisible(false);
NewGame callNewGamePanel = new NewGame();
this.add(callNewGamePanel);
callNewGamePanel.setVisible(true);
而不是
this.getParent().add(callNewGamePanel);
实际上会将第三个面板直接添加到窗口的内容中。
还要注意 Andrew Thompson 所说的默认this.add(callNewGamePanel);
,每次在窗口或其内容窗格上调用BorderLayout
时,添加的组件都将替换上一个组成部分:
f.add(面板);一个小组已经被添加到了中心 BorderLayout的。边框布局中的每个位置都可以显示最大值 一个组成部分。