我如何添加到 BorderLayout.Center?
How do I add to the BorderLayout.Center?
我无法添加到 BorderLayout 的中心。几个屏幕上什么也没有出现。此代码仍在开发中,但它是为汽车租赁服务而设计的。我们最终将我们的代码连接到 JDBC,并让数据库 return 获得相应的结果。理想情况下,该代码应该在用户使用该服务时向其显示多个屏幕。一个屏幕会让他们输入他们的信息,另一个屏幕会过滤不同的汽车选项,然后最终结果是显示由连接的数据库 return 编辑的结果。我将多个面板添加到 BorderLayout 的中心,但我最初并未将它们设置为可见。当按下按钮时,它们将变得可见。
//The following is the beginning of our code. It is not complete yet
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import java.util.Scanner;
public class Rental extends JFrame implements ActionListener, Customer, Car
{
Container cont = getContentPane();
Font myFont = new Font("tester ", Font.BOLD, 34);
BorderLayout border = new BorderLayout();
FlowLayout flow = new FlowLayout();
GridLayout grid1 = new GridLayout(0,1);
JPanel testPanel = new JPanel(); //Panel where I tried to add to center
/* myDrive logo */
JButton custom;
/*Opening Screen: */
JPanel welcomePnl = new JPanel();
JLabel titleLbl = new JLabel("");
//This is a draft summary//
JLabel info = new JLabel(" Welcome to myDrive!. myDrive is a car rental application designed for you to choose a car based on your preferences.");
JPanel btnPnl = new JPanel();
JPanel btnPnl2 = new JPanel(); //contains Car instead of begin
JCheckBox yesBox = new JCheckBox("Agree to Terms & Conditions");
JCheckBox noBox = new JCheckBox(" Do Not Agree");
/* Rental Panel--Screen 2: Fields for user to enter for rental*/
JPanel rentalPnl = new JPanel();
JTextField nameField = new JTextField("First Name");
JTextField lastName = new JTextField("Last Name");
JButton carBtn = new JButton("Car"); //continues next
/*Car Panel--Screen 3: Car panel for car options*/
JPanel carPnl = new JPanel();
JPanel brandPnl = new JPanel(); //brands
JPanel colorPnl = new JPanel(); //colors
JLabel carLbl = new JLabel("Welcome to the Car selection screen:");
JCheckBox brand1 = new JCheckBox("BMW");
JCheckBox brand2 = new JCheckBox("Mercedez");
JCheckBox brand3 = new JCheckBox("Tesla");
JCheckBox brand4 = new JCheckBox("Ford");
JCheckBox brand5 = new JCheckBox("Chrystler");
JCheckBox color1 = new JCheckBox("Black");
JCheckBox color2 = new JCheckBox("Silver");
JCheckBox color3 = new JCheckBox("Red");
JCheckBox color4 = new JCheckBox("Blue");
JCheckBox drive1 = new JCheckBox("AWD");
JCheckBox drive2 = new JCheckBox("TWD");
JButton beginBtn = new JButton("Begin");
JButton exitBtn = new JButton("Exit");
//Constructor
public Rental()
{
super("MyDrive");
cont.add(testPanel, BorderLayout.CENTER);
testPanel.setBackground(Color.PINK);
cont.setLayout(border); //border layout for the container
//Opening Screen
beginBtn.addActionListener(this);
beginBtn.setToolTipText("Being your rental process");
exitBtn.addActionListener(this);
welcomePnl.setBackground(Color.GRAY);
welcomePnl.add(titleLbl);
welcomePnl.add(info);
info.setVisible(true);
cont.add(info, BorderLayout.CENTER);
cont.add(welcomePnl, BorderLayout.NORTH);
titleLbl.setFont(myFont);
cont.add(btnPnl, BorderLayout.SOUTH);
btnPnl.setLayout(flow);
btnPnl.setBackground(Color.black);
btnPnl.add(beginBtn);
beginBtn.setVisible(false);
btnPnl.add(carBtn);
carBtn.setVisible(false);
btnPnl.add(exitBtn);
nameField.setVisible(false);
lastName.setVisible(false);
carBtn.addActionListener(this);
carBtn.setVisible(false);
rentalPnl.add(carBtn, BorderLayout.SOUTH);
rentalPnl.add(nameField);
rentalPnl.add(lastName);
rentalPnl.setLayout(flow);
cont.add(rentalPnl);
// for logo//
Icon b = new ImageIcon(getClass().getResource("myDrive.png"));
custom= new JButton(b);
welcomePnl.add(custom);
welcomePnl.add(yesBox);
welcomePnl.add(noBox);
yesBox.addActionListener(this);
noBox.addActionListener(this);
//carPnl.add(carLbl, BorderLayout.NORTH);
brandPnl.setLayout(grid1);
brandPnl.setVisible(false);
brandPnl.setBackground(Color.GRAY);
brandPnl.add(brand1);
brandPnl.add(brand2);
brandPnl.add(brand3);
brandPnl.add(brand4);
brandPnl.add(brand5);
colorPnl.setLayout(grid1);
colorPnl.setVisible(false);
colorPnl.setBackground(Color.GRAY);
colorPnl.add(color1);
colorPnl.add(color2);
colorPnl.add(color3);
colorPnl.add(color4);
carPnl.add(drive1);
carPnl.add(drive2);
carPnl.setVisible(false);
cont.add(brandPnl, border.EAST);
cont.add(colorPnl, border.WEST);
cont.add(carPnl);
carLbl.setVisible(false);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == yesBox)
{
beginBtn.setVisible(true);
}
if(source == noBox)
{
System.exit(0);
}
if(source == beginBtn)
{
welcomePnl.setVisible(false);
btnPnl.setVisible(true);
btnPnl.remove(beginBtn);
btnPnl.add(carBtn);
carBtn.setVisible(true);
rentalPnl.setVisible(true);
nameField.setVisible(true);
String fName = nameField.getText();
lastName.setVisible(true);
String lName = lastName.getText();
}
if(nameField != null & lastName != null && source == carBtn)
{
carBtn.setEnabled(true);
rentalPnl.setVisible(false);
carLbl.setVisible(true);
brandPnl.setVisible(true);
colorPnl.setVisible(true);
cont.add(carLbl, border.NORTH);
}
if(source == exitBtn)
System.exit(0);
}
//Main Method
//Sets the size of the myDrive screen- size is not resizable (cannot be changed while using program)
public static void main(String[] args)
{
Rental r1 = new Rental(); //new rental
r1.setSize(600,600);
r1.setVisible(true);
r1.setDefaultCloseOperation(EXIT_ON_CLOSE);
r1.setResizable(false);
try {
// to get a connection to the SQL database//
String dbUrl = "jcbc:mysql://localhost:3306/demo";
String user = "student";
String password= "student";
Connection myConn = DriverManager.getConnection(dbUrl, user, password);
//a statement object, to execute the SQL query
Statement myStmt = myConn.createStatement();
//insert a new employee//
System.out.println("Inserting a new employee to database\n");
//using the statement object to insert data, the executeUpdate statemnt is a method to handle any update to the database, can be used for insert, update, and deletes//
//demo code below//
/*int rowsAffected = myStmt.executeUpdate(
//"insert into employees " + "(last_name, first_name, email, department, salary)" + "values" + "('Wright, 'Eric', 'eric.wright@foo.com', 'HR', 33000.00)"
//);
"insert into employees " + "(last_name, first_name, email, department, salary)" + "values" + "(" + name2 + "," + name1 + "," + onlinemail + ")"
);
*/
//this will return a result set//
ResultSet myRs = myStmt.executeQuery("select * from employees order by last_name");
// Process the result set, reads data from each row
// getter method to print out the last and first name from the result set//
while (myRs.next())
{
System.out.println(myRs.getString("last_name") + ", " + myRs.getString(("first_name")));
}
}
catch (Exception exc) {
exc.printStackTrace();
}
/*
finally {
if (myRs != null)
{
myRs.close();
}
}
*/
}
public String getBrand()
{
return null;
}
@Override
public String getColor()
{
return null;
}
@Override
public int getYear()
{
return 0;
}
@Override
public int getMiles()
{
return 0;
}
@Override
public String getDriveType()
{
return null;
}
}
尝试在使用布局添加组件之前设置布局:
cont.setLayout(border); //border layout for the container
cont.add(testPanel, BorderLayout.CENTER);
我无法添加到 BorderLayout 的中心。几个屏幕上什么也没有出现。此代码仍在开发中,但它是为汽车租赁服务而设计的。我们最终将我们的代码连接到 JDBC,并让数据库 return 获得相应的结果。理想情况下,该代码应该在用户使用该服务时向其显示多个屏幕。一个屏幕会让他们输入他们的信息,另一个屏幕会过滤不同的汽车选项,然后最终结果是显示由连接的数据库 return 编辑的结果。我将多个面板添加到 BorderLayout 的中心,但我最初并未将它们设置为可见。当按下按钮时,它们将变得可见。
//The following is the beginning of our code. It is not complete yet
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import java.util.Scanner;
public class Rental extends JFrame implements ActionListener, Customer, Car
{
Container cont = getContentPane();
Font myFont = new Font("tester ", Font.BOLD, 34);
BorderLayout border = new BorderLayout();
FlowLayout flow = new FlowLayout();
GridLayout grid1 = new GridLayout(0,1);
JPanel testPanel = new JPanel(); //Panel where I tried to add to center
/* myDrive logo */
JButton custom;
/*Opening Screen: */
JPanel welcomePnl = new JPanel();
JLabel titleLbl = new JLabel("");
//This is a draft summary//
JLabel info = new JLabel(" Welcome to myDrive!. myDrive is a car rental application designed for you to choose a car based on your preferences.");
JPanel btnPnl = new JPanel();
JPanel btnPnl2 = new JPanel(); //contains Car instead of begin
JCheckBox yesBox = new JCheckBox("Agree to Terms & Conditions");
JCheckBox noBox = new JCheckBox(" Do Not Agree");
/* Rental Panel--Screen 2: Fields for user to enter for rental*/
JPanel rentalPnl = new JPanel();
JTextField nameField = new JTextField("First Name");
JTextField lastName = new JTextField("Last Name");
JButton carBtn = new JButton("Car"); //continues next
/*Car Panel--Screen 3: Car panel for car options*/
JPanel carPnl = new JPanel();
JPanel brandPnl = new JPanel(); //brands
JPanel colorPnl = new JPanel(); //colors
JLabel carLbl = new JLabel("Welcome to the Car selection screen:");
JCheckBox brand1 = new JCheckBox("BMW");
JCheckBox brand2 = new JCheckBox("Mercedez");
JCheckBox brand3 = new JCheckBox("Tesla");
JCheckBox brand4 = new JCheckBox("Ford");
JCheckBox brand5 = new JCheckBox("Chrystler");
JCheckBox color1 = new JCheckBox("Black");
JCheckBox color2 = new JCheckBox("Silver");
JCheckBox color3 = new JCheckBox("Red");
JCheckBox color4 = new JCheckBox("Blue");
JCheckBox drive1 = new JCheckBox("AWD");
JCheckBox drive2 = new JCheckBox("TWD");
JButton beginBtn = new JButton("Begin");
JButton exitBtn = new JButton("Exit");
//Constructor
public Rental()
{
super("MyDrive");
cont.add(testPanel, BorderLayout.CENTER);
testPanel.setBackground(Color.PINK);
cont.setLayout(border); //border layout for the container
//Opening Screen
beginBtn.addActionListener(this);
beginBtn.setToolTipText("Being your rental process");
exitBtn.addActionListener(this);
welcomePnl.setBackground(Color.GRAY);
welcomePnl.add(titleLbl);
welcomePnl.add(info);
info.setVisible(true);
cont.add(info, BorderLayout.CENTER);
cont.add(welcomePnl, BorderLayout.NORTH);
titleLbl.setFont(myFont);
cont.add(btnPnl, BorderLayout.SOUTH);
btnPnl.setLayout(flow);
btnPnl.setBackground(Color.black);
btnPnl.add(beginBtn);
beginBtn.setVisible(false);
btnPnl.add(carBtn);
carBtn.setVisible(false);
btnPnl.add(exitBtn);
nameField.setVisible(false);
lastName.setVisible(false);
carBtn.addActionListener(this);
carBtn.setVisible(false);
rentalPnl.add(carBtn, BorderLayout.SOUTH);
rentalPnl.add(nameField);
rentalPnl.add(lastName);
rentalPnl.setLayout(flow);
cont.add(rentalPnl);
// for logo//
Icon b = new ImageIcon(getClass().getResource("myDrive.png"));
custom= new JButton(b);
welcomePnl.add(custom);
welcomePnl.add(yesBox);
welcomePnl.add(noBox);
yesBox.addActionListener(this);
noBox.addActionListener(this);
//carPnl.add(carLbl, BorderLayout.NORTH);
brandPnl.setLayout(grid1);
brandPnl.setVisible(false);
brandPnl.setBackground(Color.GRAY);
brandPnl.add(brand1);
brandPnl.add(brand2);
brandPnl.add(brand3);
brandPnl.add(brand4);
brandPnl.add(brand5);
colorPnl.setLayout(grid1);
colorPnl.setVisible(false);
colorPnl.setBackground(Color.GRAY);
colorPnl.add(color1);
colorPnl.add(color2);
colorPnl.add(color3);
colorPnl.add(color4);
carPnl.add(drive1);
carPnl.add(drive2);
carPnl.setVisible(false);
cont.add(brandPnl, border.EAST);
cont.add(colorPnl, border.WEST);
cont.add(carPnl);
carLbl.setVisible(false);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == yesBox)
{
beginBtn.setVisible(true);
}
if(source == noBox)
{
System.exit(0);
}
if(source == beginBtn)
{
welcomePnl.setVisible(false);
btnPnl.setVisible(true);
btnPnl.remove(beginBtn);
btnPnl.add(carBtn);
carBtn.setVisible(true);
rentalPnl.setVisible(true);
nameField.setVisible(true);
String fName = nameField.getText();
lastName.setVisible(true);
String lName = lastName.getText();
}
if(nameField != null & lastName != null && source == carBtn)
{
carBtn.setEnabled(true);
rentalPnl.setVisible(false);
carLbl.setVisible(true);
brandPnl.setVisible(true);
colorPnl.setVisible(true);
cont.add(carLbl, border.NORTH);
}
if(source == exitBtn)
System.exit(0);
}
//Main Method
//Sets the size of the myDrive screen- size is not resizable (cannot be changed while using program)
public static void main(String[] args)
{
Rental r1 = new Rental(); //new rental
r1.setSize(600,600);
r1.setVisible(true);
r1.setDefaultCloseOperation(EXIT_ON_CLOSE);
r1.setResizable(false);
try {
// to get a connection to the SQL database//
String dbUrl = "jcbc:mysql://localhost:3306/demo";
String user = "student";
String password= "student";
Connection myConn = DriverManager.getConnection(dbUrl, user, password);
//a statement object, to execute the SQL query
Statement myStmt = myConn.createStatement();
//insert a new employee//
System.out.println("Inserting a new employee to database\n");
//using the statement object to insert data, the executeUpdate statemnt is a method to handle any update to the database, can be used for insert, update, and deletes//
//demo code below//
/*int rowsAffected = myStmt.executeUpdate(
//"insert into employees " + "(last_name, first_name, email, department, salary)" + "values" + "('Wright, 'Eric', 'eric.wright@foo.com', 'HR', 33000.00)"
//);
"insert into employees " + "(last_name, first_name, email, department, salary)" + "values" + "(" + name2 + "," + name1 + "," + onlinemail + ")"
);
*/
//this will return a result set//
ResultSet myRs = myStmt.executeQuery("select * from employees order by last_name");
// Process the result set, reads data from each row
// getter method to print out the last and first name from the result set//
while (myRs.next())
{
System.out.println(myRs.getString("last_name") + ", " + myRs.getString(("first_name")));
}
}
catch (Exception exc) {
exc.printStackTrace();
}
/*
finally {
if (myRs != null)
{
myRs.close();
}
}
*/
}
public String getBrand()
{
return null;
}
@Override
public String getColor()
{
return null;
}
@Override
public int getYear()
{
return 0;
}
@Override
public int getMiles()
{
return 0;
}
@Override
public String getDriveType()
{
return null;
}
}
尝试在使用布局添加组件之前设置布局:
cont.setLayout(border); //border layout for the container
cont.add(testPanel, BorderLayout.CENTER);