yourframename.setSize(frame.getToolkit().getDefaultToolkit().getScreenSize());
解决方案 »
- 大家讨论一下!!进者有分!
- 一个简单的线程问题!!!,请赐教,谢谢
- 我的JTextPane 不支持中文?
- 高分急求一个简单sql查询语句的解决办法...
- SqlServer JDBC的问题!在线等待.谢谢
- 我从网站down下一个JDBCProgramExCode文件,例子中有import com.sun.java.swing.*;如何得到这样的包,不然程序根本无法编译,谢谢!!
- 请问在Together的类图中attributes和properties有什么区别
- 格式化浮点数?
- 再散分。终于搞清楚了那个可恶的东西是什么了。
- 如何取得当前鼠标位置像素的颜色? 急!!!
- JBuilder7的消息提示?
- Java应该是个体系问题,我是个C++程序员,想学JAVA,但是不知道如何能学好这个体系,各位请帮忙!
yourFrame.setBounds(0,0,screenSize.width,screenSize.height);
import java.awt.*;public class CentralFrame extends JFrame{
public CentralFrame(){
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
setBounds((d.width-d.width/2)/2,(d.height-d.height/2)/2,d.width/2,d.height/2);
setVisible(true);
}
public static void main(String[] args){
CentralFrame frame=new CentralFrame();
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class ClientUser extends JFrame implements ActionListener
{
private JPanel contentPane;
private JPanel jPanel1 = new JPanel();
private JPanel jPanel2 = new JPanel();
private JScrollPane jScrollPane1 = new JScrollPane(); private JMenuBar mnuBar = new JMenuBar();
private JMenu mnuFile = new JMenu("File");
private JMenuItem mnuFileQuit = new JMenuItem ("Exit"); private JMenu mnuHelp = new JMenu("Help");
private JTable jTable1 = new JTable();
private JComboBox jComboBox1 = new JComboBox();
private JComboBox jComboBox2 = new JComboBox();
private JLabel startLabel = new JLabel();
private JLabel endLabel = new JLabel();
private JPanel jPanel3 = new JPanel();
private JTextField ticketText = new JTextField();
private JLabel orderLabel = new JLabel();
private BorderLayout borderLayout1 = new BorderLayout();
private JButton showButton = new JButton();
private JButton orderButton = new JButton();
private JButton exitButton = new JButton();
private GridBagLayout gridBagLayout1 = new GridBagLayout();
private GridBagLayout gridBagLayout2 = new GridBagLayout();
private GridBagLayout gridBagLayout3 = new GridBagLayout(); String[] columnName= {"Flight number", "Origin airport", "Destination airport", "Carrier", "Price", "Day",
"Time", "Duration","Available seats"};
QxbirdTableModel tableModel;
public ClientUser()
{
//setSelectStation();
//setShowTable(); contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(800,400));
this.setTitle("Frame Title");
jPanel1.setBackground(Color.orange);
jPanel1.setBorder(BorderFactory.createLoweredBevelBorder());
jPanel1.setLayout(gridBagLayout3);
jPanel2.setBackground(Color.orange);
jPanel2.setBorder(BorderFactory.createLoweredBevelBorder());
jPanel2.setLayout(gridBagLayout1);
startLabel.setText("Departure Station");
endLabel.setText("Destination Station");
jPanel3.setBackground(Color.orange);
jPanel3.setBorder(BorderFactory.createEtchedBorder());
jPanel3.setLayout(gridBagLayout2);
orderLabel.setText("Order The Ticket Here");
showButton.setText("Search");
orderButton.setText("Order");
orderButton.addActionListener(this);
exitButton.setText("Exit System");
exitButton.addActionListener(this); mnuFileQuit.addActionListener(this);
mnuFile.add(mnuFileQuit);
mnuBar.add(mnuFile);
this.setJMenuBar( mnuBar ); contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jScrollPane1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 559, 393));
contentPane.add(jPanel2, BorderLayout.WEST);
jPanel2.add(jPanel3, new GridBagConstraints(0, 5, 1, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(8, 2, 0, 0), 10, 8));
jPanel3.add(orderButton, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(11, 124, 5, 10), 20, -2));
jPanel3.add(orderLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(21, 76, 0, 10), 11, 18));
jPanel3.add(ticketText, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 64, 0, 10), 143, 0));
jPanel2.add(startLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 116, 0, 8), 9, 9));
jPanel2.add(endLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 104, 0, 12), 9, 9));
jPanel2.add(jComboBox1, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 7), 73, 0));
jPanel2.add(showButton, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(19, 132, 0, 10), 34, -2));
jPanel2.add(jComboBox2, new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 9), 71, 0));
jPanel2.add(exitButton, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(80, 55, 5, 58), 17, -2)); Dimension screenRect = Toolkit.getDefaultToolkit().getScreenSize();
int centerWidth = screenRect.width < this.getSize().width ?
screenRect.width : screenRect.width/2 - this.getSize().width/2;
int centerHeight = screenRect.height < this.getSize().height ?
screenRect.height : screenRect.height/2 - this.getSize().height/2; //this.setLocation(centerWidth, centerHeight);
//contentPane.setLocation(0, 0);
//contentPane.setSize(jPanel1.getToolkit().getDefaultToolkit().getScreenSize());
this.setBounds(0,0,screenRect.width,screenRect.height); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
} public static void main(String[] args)
{
ClientUser cu = new ClientUser();
}
public void actionPerformed(ActionEvent e)
{}
private class QxbirdTableModel extends DefaultTableModel
{
QxbirdTableModel(String[] columnName,int columnNo)
{
super(columnName,columnNo);
}
public boolean isCellEditable(int row, int col)
{
return false;
}
}
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds(0,0,screenSize.width,screenSize.height);
setExtendedState(JFrame.MAXIMIZED_BOTH);
这50分可不够了,你得再给100分~~
这是调试好的代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class ClientUser extends JFrame implements ActionListener
{
private JPanel contentPane;
private JPanel jPanel1 = new JPanel();
private JPanel jPanel2 = new JPanel();
private JScrollPane jScrollPane1 = new JScrollPane(); private JMenuBar mnuBar = new JMenuBar();
private JMenu mnuFile = new JMenu("File");
private JMenuItem mnuFileQuit = new JMenuItem ("Exit"); private JMenu mnuHelp = new JMenu("Help");
private JTable jTable1 = new JTable();
private JComboBox jComboBox1 = new JComboBox();
private JComboBox jComboBox2 = new JComboBox();
private JLabel startLabel = new JLabel();
private JLabel endLabel = new JLabel();
private JPanel jPanel3 = new JPanel();
private JTextField ticketText = new JTextField();
private JLabel orderLabel = new JLabel();
private BorderLayout borderLayout1 = new BorderLayout();
private JButton showButton = new JButton();
private JButton orderButton = new JButton();
private JButton exitButton = new JButton();
private GridBagLayout gridBagLayout1 = new GridBagLayout();
private GridBagLayout gridBagLayout2 = new GridBagLayout();
private GridBagLayout gridBagLayout3 = new GridBagLayout(); String[] columnName= {"Flight number", "Origin airport", "Destination airport", "Carrier", "Price", "Day",
"Time", "Duration","Available seats"};
QxbirdTableModel tableModel;
public ClientUser()
{
//setSelectStation();
//setShowTable(); contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
//this.setSize(new Dimension(800,400));
this.setTitle("Frame Title");
jPanel1.setBackground(Color.orange);
jPanel1.setBorder(BorderFactory.createLoweredBevelBorder());
jPanel1.setLayout(gridBagLayout3);
jPanel2.setBackground(Color.orange);
jPanel2.setBorder(BorderFactory.createLoweredBevelBorder());
jPanel2.setLayout(gridBagLayout1);
startLabel.setText("Departure Station");
endLabel.setText("Destination Station");
jPanel3.setBackground(Color.orange);
jPanel3.setBorder(BorderFactory.createEtchedBorder());
jPanel3.setLayout(gridBagLayout2);
orderLabel.setText("Order The Ticket Here");
showButton.setText("Search");
orderButton.setText("Order");
orderButton.addActionListener(this);
exitButton.setText("Exit System");
exitButton.addActionListener(this); mnuFileQuit.addActionListener(this);
mnuFile.add(mnuFileQuit);
mnuBar.add(mnuFile);
this.setJMenuBar( mnuBar ); contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jScrollPane1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 559, 393));
contentPane.add(jPanel2, BorderLayout.WEST);
jPanel2.add(jPanel3, new GridBagConstraints(0, 5, 1, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(8, 2, 0, 0), 10, 8));
jPanel3.add(orderButton, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(11, 124, 5, 10), 20, -2));
jPanel3.add(orderLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(21, 76, 0, 10), 11, 18));
jPanel3.add(ticketText, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 64, 0, 10), 143, 0));
jPanel2.add(startLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 116, 0, 8), 9, 9));
jPanel2.add(endLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 104, 0, 12), 9, 9));
jPanel2.add(jComboBox1, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 7), 73, 0));
jPanel2.add(showButton, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(19, 132, 0, 10), 34, -2));
jPanel2.add(jComboBox2, new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 20, 0, 9), 71, 0));
jPanel2.add(exitButton, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(80, 55, 5, 58), 17, -2)); Dimension screenRect = Toolkit.getDefaultToolkit().getScreenSize();
int centerWidth = screenRect.width < this.getSize().width ?
screenRect.width : screenRect.width/2 - this.getSize().width/2;
int centerHeight = screenRect.height < this.getSize().height ?
screenRect.height : screenRect.height/2 - this.getSize().height/2; //this.setLocation(centerWidth, centerHeight);
//contentPane.setLocation(0, 0);
//contentPane.setSize(jPanel1.getToolkit().getDefaultToolkit().getScreenSize());
this.setBounds(0,0,screenRect.width,screenRect.height); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds(0,0,screenSize.width,screenSize.height); this.setVisible(true);
} public static void main(String[] args)
{
ClientUser cu = new ClientUser();
}
public void actionPerformed(ActionEvent e)
{}
private class QxbirdTableModel extends DefaultTableModel
{
QxbirdTableModel(String[] columnName,int columnNo)
{
super(columnName,columnNo);
}
public boolean isCellEditable(int row, int col)
{
return false;
}
}
}
this.pack();之后再设定窗体的大小,这就是pack()的作用,this.pack()之前的所有窗体大小设定是不管用的。
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
yourFrame.setBounds(0,0,screenSize.width,screenSize.height);
(可能是复制的时候有问题)
setExtendedState明明是你自己的你倒来问我~~郁闷
setExtendedState是javax.swing.JFrame实例的方法,参数是javax.swing.JFrame.MAXIMIZED_BOTH