java GUI 问题 我该怎么设置页面布局使得组件在窗体放大时也在中间显示?
publicclassMainFrameextendsJFrameimplementsActionListener{/****/privatestaticfinallon...
public class MainFrame extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel welcome = new JLabel("通讯录");
private JPanel welcomejpanel=new JPanel();
private JButton sureJButton=new JButton("welcome");
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth=screenSize.width;
int srceenHeight=screenSize.height;
public MainFrame()
{
//设置窗体相关属性
//设置窗口标题
this.setTitle("--个人通讯录--");
//设置窗口可拖动大小
this.setResizable(true);
//设置窗口大小
this.setSize(screenWidth/2,srceenHeight/2);
//设置窗口居于屏幕中央
setLocation((screenWidth-getWidth())/2,(srceenHeight-getHeight())/2);
//设置面板容器的布局策略为空
welcomejpanel.setLayout(null);
//设置面板容器背景色
welcomejpanel.setBackground(Color.BLUE);
sureJButton.setBounds(220, 250, 210, 110);
welcome.setBounds(250, 50, 200, 100);
//设置按钮的文本颜色
sureJButton.setForeground(Color.RED);
welcome.setForeground(Color.BLACK);
//设置标签和两个按钮的字体
sureJButton.setFont(new Font("宋体",Font.PLAIN,50));
welcome.setFont(new Font("楷体",Font.PLAIN,50));
//为按钮添加动作事件监听器
sureJButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
MainTest.main(new String[0]);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
new MainTest().validate();
MainFrame.this.dispose();
}
});
//将面板容器添加到内容窗格
this.add(welcomejpanel);
welcomejpanel.add(welcome);
welcomejpanel.add(sureJButton);
//为窗口添加关闭响应事件
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//显示窗体
this.setVisible(true);
}
//主方法
public static void main(String []args)
{
new MainFrame();//创建登陆窗体
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
} 展开
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel welcome = new JLabel("通讯录");
private JPanel welcomejpanel=new JPanel();
private JButton sureJButton=new JButton("welcome");
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth=screenSize.width;
int srceenHeight=screenSize.height;
public MainFrame()
{
//设置窗体相关属性
//设置窗口标题
this.setTitle("--个人通讯录--");
//设置窗口可拖动大小
this.setResizable(true);
//设置窗口大小
this.setSize(screenWidth/2,srceenHeight/2);
//设置窗口居于屏幕中央
setLocation((screenWidth-getWidth())/2,(srceenHeight-getHeight())/2);
//设置面板容器的布局策略为空
welcomejpanel.setLayout(null);
//设置面板容器背景色
welcomejpanel.setBackground(Color.BLUE);
sureJButton.setBounds(220, 250, 210, 110);
welcome.setBounds(250, 50, 200, 100);
//设置按钮的文本颜色
sureJButton.setForeground(Color.RED);
welcome.setForeground(Color.BLACK);
//设置标签和两个按钮的字体
sureJButton.setFont(new Font("宋体",Font.PLAIN,50));
welcome.setFont(new Font("楷体",Font.PLAIN,50));
//为按钮添加动作事件监听器
sureJButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
MainTest.main(new String[0]);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
new MainTest().validate();
MainFrame.this.dispose();
}
});
//将面板容器添加到内容窗格
this.add(welcomejpanel);
welcomejpanel.add(welcome);
welcomejpanel.add(sureJButton);
//为窗口添加关闭响应事件
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//显示窗体
this.setVisible(true);
}
//主方法
public static void main(String []args)
{
new MainFrame();//创建登陆窗体
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询