这个传说中java最难得布局是要真么搞???求大神。。。
话不多说,先上图上代码。。。这是我自己写的界面,用了网格组的布局方式,下面上代码(注:关于布局的代码我用蓝色标注)
package JNotePad;
import java.awt.*;
import javax.swing.*;
public class JNotePad {
JNotePad(){
JFrame jframe = new JFrame("HSQL Database Manager");
jframe.setSize(800,600);
GridBagLayout gridBagLayout = new GridBagLayout();
jframe.setLayout(gridBagLayout);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
JMenu jfile = new JMenu("File");
JMenuItem conn = new JMenuItem("Connect...");
JMenuItem open = new JMenuItem("Open Script...");
JMenuItem sasc = new JMenuItem("Save Script...");
JMenuItem sare = new JMenuItem("Save Result...");
JMenuItem exit = new JMenuItem("Exit");
jfile.add(conn);
jfile.addSeparator();
jfile.add(open);
jfile.add(sasc);
jfile.add(sare);
jfile.addSeparator();
jfile.add(exit);
JMenu jview = new JMenu("View");
JMenuItem retr = new JMenuItem("Refresh Tree");
JMenuItem auto = new JCheckBoxMenuItem("Auto-refresh tree");
JMenuItem show1 = new JCheckBoxMenuItem("Show row counts");
JMenuItem show2 = new JCheckBoxMenuItem("Show system tables");
JMenuItem show3 = new JCheckBoxMenuItem("Show schemas");
JMenuItem show4 = new JCheckBoxMenuItem("Show results in grid(a.o.t. text) Ctrl-G");
jview.add(retr);
jview.add(auto);
jfile.addSeparator();
jview.add(show1);
jview.add(show2);
jview.add(show3);
jview.add(show4);
JMenu jcomm = new JMenu("Command");
JMenuItem select = new JMenuItem("SELECT");
JMenuItem insert = new JMenuItem("INSERT");
JMenuItem update = new JMenuItem("UPDATE");
JMenuItem delete = new JMenuItem("DELETE");
JMenuItem excute = new JMenuItem("EXCUTE");
JMenuItem table = new JMenuItem("CREATE TABLE");
JMenuItem drop = new JMenuItem("DROP TABLE");
JMenuItem cr_index = new JMenuItem("CREATE INDEX");
JMenuItem dr_index = new JMenuItem("DROP INDEX");
JMenuItem commit = new JMenuItem("COMMIT*");
JMenuItem roll = new JMenuItem("ROLL BACK*");
JMenuItem check = new JMenuItem("CHECHPOINT*");
JMenuItem script = new JMenuItem("SCRIPT");
JMenuItem set = new JMenuItem("SET");
JMenuItem shut = new JMenuItem("SHUTDOWN");
JMenuItem test = new JMenuItem("Test Script");
jcomm.add(select);
jcomm.add(insert);
jcomm.add(update);
jcomm.add(delete);
jcomm.add(excute);
jfile.addSeparator();
jcomm.add(table);
jcomm.add(drop);
jcomm.add(cr_index);
jcomm.add(dr_index);
jfile.addSeparator();
jcomm.add(commit);
jcomm.add(roll);
jcomm.add(check);
jcomm.add(check);
jcomm.add(script);
jcomm.add(set);
jcomm.add(shut);
jcomm.add(test);
JMenu jrece = new JMenu("Recent");
JMenu jopti = new JMenu("Options");
JMenu jtool = new JMenu("Tools");
bar.add(jfile);
bar.add(jview);
bar.add(jcomm);
bar.add(jrece);
bar.add(jopti);
bar.add(jtool);
JTree jtree = new JTree();
JScrollPane jplA =
new JScrollPane(jtree,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
GridBagConstraints constraints1 = new GridBagConstraints();
constraints1.gridx = 0;
constraints1.gridy = 0;
constraints1.gridwidth = 2;
constraints1.gridheight = 7;
constraints1.weightx = 100;
constraints1.weighty = 100;
constraints1.anchor = constraints1.NORTHWEST;
constraints1.fill = constraints1.BOTH;
JButton bt1 = new JButton("Clear");
GridBagConstraints constraints2 = new GridBagConstraints();
constraints2.gridx = 2;
constraints2.gridy = 0;
constraints2.gridwidth = 1;
constraints2.gridheight = 1;
constraints2.weightx = 100;
constraints2.weighty = 100;
constraints2.anchor = constraints2.CENTER;
constraints2.fill = constraints2.NONE;
JButton bt2 = new JButton("Execute");
GridBagConstraints constraints3 = new GridBagConstraints();
constraints3.gridx = 6;
constraints3.gridy = 0;
constraints3.gridwidth = 1;
constraints3.gridheight = 1;
constraints3.weightx = 100;
constraints3.weighty = 100;
constraints3.anchor = constraints3.CENTER;
constraints3.fill = constraints3.NONE;
JTextArea text1 = new JTextArea();
text1.insert("swdewfrefgr",0);
JScrollPane jplB = new JScrollPane(text1,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
GridBagConstraints constraints4 = new GridBagConstraints();
constraints4.gridx = 3;
constraints4.gridy = 0;
constraints4.gridwidth = 3;
constraints4.gridheight = 2;
constraints4.weightx = 100;
constraints4.weighty = 100;
constraints4.anchor = constraints4.CENTER;
constraints4.fill = constraints4.BOTH;
JTextArea text2 = new JTextArea();
text2.insert("swdewfrefgr",0);
JScrollPane jplC = new JScrollPane(text2,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
GridBagConstraints constraints5 = new GridBagConstraints();
constraints5.gridx = 2;
constraints5.gridy = 2;
constraints5.gridwidth = 5;
constraints5.gridheight = 5;
constraints5.weightx = 100;
constraints5.weighty = 100;
constraints5.anchor = constraints5.CENTER;
constraints5.fill = constraints5.BOTH;
jframe.add(jplA,constraints1);
jframe.add(bt1,constraints2);
jframe.add(bt2,constraints3);
jframe.add(jplB,constraints4);
jframe.add(jplC,constraints5);
jframe.setJMenuBar(bar);
jframe.setVisible(true);
}
public static void main(String[] args) {
JNotePad notepad = new JNotePad();
}
}
但这不是我想要的效果,总是差一点,完美的效果图如下:
我已经在自己的基础上尝试修改,但总是无法达到效果,希望擅长布局的大神们能给一点指导,感激。。。 Java 布局 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- menu
左中右 north
左west
下 south
反正winform的grid好像是这样布局的。。这样子应该可以设置大小才对啊 --------------------编程问答-------------------- 最近在玩SWT的路过。。。SWING,这个,不懂 --------------------编程问答-------------------- 感觉你这个布局也不算复杂,其实布局就是方位布局最有用,只是各种嵌套的问题而已。
右边是个panel,布局是方位布局,设置上一个panel下一个选项卡 上的角的panel也用方位布局,左中右,左右两个按钮,中间一个框,下面的选项卡也是方位布局,但是里面只有中,然后输入框。 --------------------编程问答--------------------
JFrame上的感觉不好用方位布局,左边一个panel没问题,右边一个panel,这个panel分上下,上边又一个panel,下边又一个panel,都没问题。。。但在JFrame中有左右两个panel,中间没有东西,在窗口变化大小的时候,中间就会空出来,这个要怎么解决??? --------------------编程问答-------------------- 如果你会用TableLayout的话,实现这个布局非常简单,只要几行简单的代码,效果如图:
package com.tur.demo;--------------------编程问答--------------------
import info.clearthought.layout.TableLayout;
import javax.swing.*;
import java.awt.*;
public class Hello {
private static void layoutComponents(Container container) {
double f = TableLayout.FILL;
double p = TableLayout.PREFERRED;
double[] columnSize = {180, p, f, p}; // 蓝易做图域的宽
double[] rowSize = {120, f}; // 红易做图域的高
TableLayout layout = new TableLayout(columnSize, rowSize);
container.setLayout(layout);
JPanel panel = null;
panel = new JPanel();
panel.setBackground(Color.BLUE);
container.add(panel, "0, 0, 0, 1");
container.add(new JButton("Clear"), "1, 0");
panel = new JPanel();
panel.setBackground(Color.RED);
container.add(panel, "2, 0");
container.add(new JButton("Execute"), "3, 0");
panel = new JPanel();
panel.setBackground(Color.GREEN);
container.add(panel, "1, 1, 3, 1");
}
private static void createGuiAndShow() {
JFrame frame = new JFrame("");
layoutComponents(frame.getContentPane());
// Set frame's close operation and location in the screen.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
createGuiAndShow();
}
}
double[] columnSize = {180, p, f, p}; // 180为蓝易做图域的宽--------------------编程问答-------------------- 围观。。 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 看看 --------------------编程问答-------------------- 嗯嗯。。。首先感谢六楼十分详细的讲解,效果图以及代码都十分完备。
double[] rowSize = {120, f}; // 120为红易做图域的高
不过我看了TableLayout以及import info.clearthought.layout.TableLayout,我发现这个在Java的SE的API中是没有的,这个应该是android的界面设计,所以我就没法用上了。。。嗯嗯,还是要感谢六楼的建议。希望有更多的指导。。。 --------------------编程问答-------------------- 除 --------------------编程问答--------------------
TableLayout是Java的,非Android,下载连接:http://search.maven.org/#search%7Cga%7C1%7Ctablelayout --------------------编程问答-------------------- 学习了。。。谢大神!!! --------------------编程问答-------------------- 呵呵,学习!!! --------------------编程问答-------------------- GridBagLayout偏重整体设计,万一其中其中某个地方需要调整,那不得整体重新设计?!
所以我一般不用这个。我一般自己设计layout的子类。 --------------------编程问答-------------------- 这是Java最简单的. --------------------编程问答-------------------- 设计好之后,按上下左右添加而已。
另外,网上应该有不少例子吧。 --------------------编程问答-------------------- 拿WB,全搞定~ --------------------编程问答--------------------
--------------------编程问答-------------------- 这个我已经回答了会出现的问题。。。还来应用要不说说怎么解决我说的出现的问题 --------------------编程问答-------------------- 嘎嘎!初来咋到,观望学习! --------------------编程问答-------------------- 表示本人不太爱用布局管理器 都是手动
补充:Java , Java SE