在一个JFrame中想布局左边的panel为640X480大小,右边的Panel为150X480,要怎么布局合适? 我就是用的BoderLayout,那怎样设置panel的大小能生效呢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 getContentPane().add(panel1,BoderLayout.WEST);getContentPane().add(panel2,BoderLayout.EAST);panel1.setBounds(x, y, 680, 480);panel2.setBounds(x,y,150,480);最后要打包pack(); 把布局设为null,然后用setBounds设置绝对位置,位置大小都由自己定.不过这样应用程序变化的时候就不一定好看了还是用JScrollPane来的妥当 把代码贴出来,各位请帮忙看一下!import javax.swing.*;import java.awt.event.*;import java.awt.*;public class TestFrame extends JFrame { static final int FRAME_WIDTH = 800; static final int FRAME_HTIGHT = 600; private JDesktopPane desktop; JPanel leftPane,rightPane; public TestFrame() { super("窗体"); setBounds(0,0,FRAME_WIDTH,FRAME_HTIGHT); setDefaultLookAndFeelDecorated(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(true); Container contentPane = getContentPane(); leftPane = new JPanel(); leftPane.setLayout(null); leftPane.setBounds(0,0,640,480); leftPane.setBorder(BorderFactory.createTitledBorder( BorderFactory.createLineBorder(Color.black), "Work Area")); desktop = new JDesktopPane(); desktop.setLocation(20,20); desktop.setSize(640,480); leftPane.add(desktop); rightPane = new JPanel(); rightPane.setLayout(null); rightPane.setBorder(BorderFactory.createTitledBorder( BorderFactory.createLineBorder(Color.black), "Info Area")); rightPane.setSize(150,480); contentPane.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1; gbc.weighty = 1; gbc.insets = new Insets(1,1,1,1); add(contentPane,leftPane,gbc,0,1,1,1); add(contentPane,rightPane,gbc,1,1,1,1); /* contentPane.add(leftPane,BorderLayout.CENTER); contentPane.add(rightPane,BorderLayout.EAST); */ } private void add(Container cn,Component c, GridBagConstraints gbc, int x, int y, int w, int h){ gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = w; gbc.gridheight = h; cn.add(c, gbc); } public static void main(String[] args) { TestFrame testFrame = new TestFrame(); testFrame.setVisible(true); }} getContentPane().setLayout(null);之后才能自己布局 to beyond_xiruo:我不太明白你的意思!我在上面的代码中已经设定了leftPane 和rightPane的Layout和位置,如:leftPane = new JPanel();leftPane.setLayout(null);leftPane.setBounds(0,0,640,480);那如果getContentPane().setLayout(null);那我最后的整个布局要怎么做呢?我不是在JB中写代码,所以对于在JB中的布局我不熟悉,我就是在文本编辑器里写!所以请您说的清楚些!谢谢! setBounds(x, y, width, height) only works in setLayout(null) leftPane.setBounds(0,0,640,480);这句是在JFrame上布置的,所以一定要getContentPane().setLayout(null);setBounds才能起作用. 正则表达式问题 JAVA 調用EXE程序 并打開指定文件 如何修正这个错误.菜鸟求教 超出合法范围的值赋给一 变量,该变量的值如何?有请 大神来看下,问什么照着书上的例子写到myecliose报错?? 关于字节流和字符流的一点小疑问 简单问题 求 rose license 今天结贴 2个问题,关于文件操作 java新手!!关于用protected修饰的成员内部类的继承问题!!求大神指教!! 紧急求助 java 查询mysql数据库问题???
getContentPane().add(panel2,BoderLayout.EAST);
panel1.setBounds(x, y, 680, 480);
panel2.setBounds(x,y,150,480);
最后要打包
pack();
不过这样应用程序变化的时候就不一定好看了
还是用JScrollPane来的妥当
import java.awt.event.*;
import java.awt.*;public class TestFrame extends JFrame {
static final int FRAME_WIDTH = 800;
static final int FRAME_HTIGHT = 600;
private JDesktopPane desktop;
JPanel leftPane,rightPane;
public TestFrame() {
super("窗体");
setBounds(0,0,FRAME_WIDTH,FRAME_HTIGHT);
setDefaultLookAndFeelDecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true); Container contentPane = getContentPane();
leftPane = new JPanel();
leftPane.setLayout(null);
leftPane.setBounds(0,0,640,480);
leftPane.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.black),
"Work Area"));
desktop = new JDesktopPane();
desktop.setLocation(20,20);
desktop.setSize(640,480);
leftPane.add(desktop);
rightPane = new JPanel();
rightPane.setLayout(null);
rightPane.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.black),
"Info Area"));
rightPane.setSize(150,480);
contentPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.insets = new Insets(1,1,1,1);
add(contentPane,leftPane,gbc,0,1,1,1);
add(contentPane,rightPane,gbc,1,1,1,1);
/*
contentPane.add(leftPane,BorderLayout.CENTER);
contentPane.add(rightPane,BorderLayout.EAST);
*/
}
private void add(Container cn,Component c, GridBagConstraints gbc, int x, int y, int w, int h){
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
cn.add(c, gbc);
} public static void main(String[] args) {
TestFrame testFrame = new TestFrame();
testFrame.setVisible(true);
}
}
我在上面的代码中已经设定了leftPane 和rightPane的Layout和位置,如:
leftPane = new JPanel();
leftPane.setLayout(null);
leftPane.setBounds(0,0,640,480);
那如果getContentPane().setLayout(null);那我最后的整个布局要怎么做呢?
我不是在JB中写代码,所以对于在JB中的布局我不熟悉,我就是在文本编辑器里写!
所以请您说的清楚些!谢谢!
这句是在JFrame上布置的,所以一定要getContentPane().setLayout(null);
setBounds才能起作用.