FlowLayout布局的特点是可以根据窗体大小的变化改变组件的位置。
本来我的JFrame上面只有一个JPanel,上面放了三个按钮,改变窗体的大小三个按钮可以自动换行。
现在我在上面JFrame又加了一个JPanel,上面什么也没有,然后通过JSplitPane将二者左右结合起来再放到JFrame上面。
但是问题出现了,现在三个按钮只能固定的排成一行,拖动中间分隔条时也无法自动换行。
恳请各位高手支招,这是我参加一个比赛的作品里面需要用到的一个功能,明天就要提交,很急。拜托了,各位,答案出现马上给分,不胜感激!(相关代码如下:)import javax.swing.*;
import java.awt.*;
public class TestLayout extends JFrame{
public TestLayout() {
Container cp = getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel2.setBackground(Color.blue);
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JButton b3 = new JButton("Button 2");
panel1.add(b1);
panel1.add(b2);
panel1.add(b3);
JSplitPane sp = new JSplitPane(1, true, panel1, panel2); //两个面板结合
cp.add(sp);
setBounds(200,150,400,200);
setVisible(true);
}
public static void main(String[] args) {
TestLayout frm = new TestLayout();
}
}
本来我的JFrame上面只有一个JPanel,上面放了三个按钮,改变窗体的大小三个按钮可以自动换行。
现在我在上面JFrame又加了一个JPanel,上面什么也没有,然后通过JSplitPane将二者左右结合起来再放到JFrame上面。
但是问题出现了,现在三个按钮只能固定的排成一行,拖动中间分隔条时也无法自动换行。
恳请各位高手支招,这是我参加一个比赛的作品里面需要用到的一个功能,明天就要提交,很急。拜托了,各位,答案出现马上给分,不胜感激!(相关代码如下:)import javax.swing.*;
import java.awt.*;
public class TestLayout extends JFrame{
public TestLayout() {
Container cp = getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel2.setBackground(Color.blue);
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JButton b3 = new JButton("Button 2");
panel1.add(b1);
panel1.add(b2);
panel1.add(b3);
JSplitPane sp = new JSplitPane(1, true, panel1, panel2); //两个面板结合
cp.add(sp);
setBounds(200,150,400,200);
setVisible(true);
}
public static void main(String[] args) {
TestLayout frm = new TestLayout();
}
}
因为你右边没有东西撑住。这篇文章看不懂我帮你改你的例子
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;public class TestLayout extends JFrame {
public TestLayout() {
Container cp = getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel(); panel2.setBackground(Color.blue);
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JButton b3 = new JButton("Button 2");
panel1.add(b1);
panel1.add(b2);
panel1.add(b3);
final JSplitPane sp = new JSplitPane(1, true, new JScrollPane(panel1),
new JScrollPane(panel2)); // 两个面板结合 sp.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY,
new PropertyChangeListener() { @Override
public void propertyChange(PropertyChangeEvent e) {
panel1.setPreferredSize(new Dimension(sp
.getDividerLocation(), panel1.getSize().height));
panel1.revalidate();
panel1.repaint();
}
});
cp.add(sp);
setBounds(200, 150, 400, 200);
setVisible(true);
} public static void main(String[] args) {
new TestLayout();
}
}
改了一下,虽然还有些小问题,自己去看研究吧。呵呵,还来得及吗?
有没有不通过JScrollPane,直接添加到JSplitPane的方法?
这是JPanel的里面一个方法在作祟。默认的时候,这个方法被调用过了,在你初始化之后,所以也就无法拖动。具体的还是给你代码,自己体会一下才有趣,我若是说的太直白你也不会喜欢的。自己多研究研究。
public class TestLayout extends JFrame {
public TestLayout() {
Container cp = getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel1 = new JPanel();
panel1.setMinimumSize(new Dimension(0, 0));
JPanel panel2 = new JPanel(); panel2.setBackground(Color.blue);
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JButton b3 = new JButton("Button 2");
panel1.add(b1);
panel1.add(b2);
panel1.add(b3);
final JSplitPane sp = new JSplitPane(1, true, panel1, panel2); // 两个面板结合 sp.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY,
new PropertyChangeListener() { @Override
public void propertyChange(PropertyChangeEvent e) {
panel1.setPreferredSize(new Dimension(sp
.getDividerLocation() - sp.getDividerSize(),
panel1.getSize().height));
panel1.revalidate();
panel1.repaint();
}
});
cp.add(sp);
setBounds(200, 150, 400, 200);
setVisible(true);
} public static void main(String[] args) {
new TestLayout();
}
}
软件讲究的人性化中的一个细节就是,一定要让用户可以方便的查看,你想,若是你将左侧的部分放的很小,会如何?滚动条的作用就是即使放的比较小,也可以通过滚动条看到全部。
或者一个替代的方法,你可以,手动的设置一个合适的minimumsize,小到一定的程度就不能继续小了。但即使这样某些时候也还是需要滚动条。