java 通过单击改变窗体大小

在窗体上放两个JPanel,jpanel1,jpanel2;jpanel1上面放JButton,单击JButton窗体大小等于就jpanel1.width+jpanel2... 在窗体上放两个JPanel,jpanel1,jpanel2;jpanel1上面放JButton,单击JButton窗体大小等于
就jpanel1.width+jpanel2.width=jFrame.width.
再在jpanel2上面放一个jbutton,单击后窗体的宽度等于jpanel1的宽度。
项目是在netbeans6.9.1上面开发的。使用的是swing控件。
谢谢
展开
 我来答
阿基米糊
2011-04-19 · 喜欢阅读,喜欢随笔,喜欢分享
阿基米糊
采纳数:52 获赞数:208

向TA提问 私信TA
展开全部
package com;

import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class T1 extends JFrame {
private static T1 thisClass = new T1();
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JPanel jPanel1 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(new Rectangle(150, 1, 150, 130));
jPanel.add(getJButton(), null);
}
return jPanel;
}
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new JPanel();
jPanel1.setLayout(null);
jPanel1.setBounds(new Rectangle(0, 0, 150, 130));
jPanel1.add(getJButton1(), null);
}
return jPanel1;
}

private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(5, 5, 92, 30));
jButton.setText("按键2");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
thisClass.setBounds(new Rectangle(0,0,160,200));
}
});
}
return jButton;
}

private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(5, 5, 67, 36));
jButton1.setText("按键1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
thisClass.setBounds(new Rectangle(0,0,320,200));
}
});
}
return jButton1;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {

thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public T1() {
super();
initialize();
}
private void initialize() {
this.setSize(160, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.setResizable(false);
}

private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJPanel(), null);
jContentPane.add(getJPanel1(), null);
}
return jContentPane;
}
}
明慧又耐心的小板栗6200
2011-04-19 · TA获得超过165个赞
知道答主
回答量:123
采纳率:0%
帮助的人:70.2万
展开全部
package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test3 {

JFrame jf;
JPanel jp1,jp2;
JButton jb1,jb2;

public Test3() {

//jp1
jp1 = new JPanel();
jp1.setPreferredSize(new Dimension(300,100));
jp1.setBorder(BorderFactory.createLineBorder(Color.black));//为JPanel添加有色边框,便于区分JPanel大小
jb1 = new JButton("jb1");
jb1.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {

/*
* jb1点击事件
*
* jpanel1.width+jpanel2.width=jFrame.width 考虑窗体的边框后入下
*/

jf.setSize((jp1.getWidth()+jp2.getWidth()+16),jp1.getHeight()+38);
}

});
jp1.add(jb1);

//jp2
jp2 = new JPanel();
jp2.setPreferredSize(new Dimension(300,100));
jp2.setBorder(BorderFactory.createLineBorder(Color.red));
jb2 = new JButton("jb2");
jb2.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {

/*
* jb2点击事件
*/

jf.setSize((jp1.getWidth()+16),(jp1.getHeight()+jp2.getHeight()+38));
}

});
jp2.add(jb2);

jf = new JFrame();
jf.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
jf.setSize(316, 238);
jf.add(jp1);
jf.add(jp2);
jf.setVisible(true);
}

public static void main(String[] args) {

new Test3();
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式