请问如何实现以下的布局? 请问这个布局应该如何实现?左边的“主角能否通过”是一个文字说明,不可以点击,只是起到一个说明的作用,具体的功能需要通过右边的下拉框选择。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 BorderLayout 布局管理器吧..不知有没正确理解楼主的意思? 就是说行分成两部分,左边一半是文字说明,右边一般是一个下拉列表,用户可以在右边的下拉列表中选择。BorderLayout我试了,实现这种功能不知道怎么弄。难道把左边添加一个Panel,然后add(new Label("主角能否通过")) ,然后在右边在添加JComboBox这样来做么? LZ尝试过自定义布局吗?用JPanel也没错啊,你可以先都new出来。但是要设置setvisible(false),通过了就设置为true嘛、 Panel passPanel = new Panel(new BorderLayout()) ; Panel passPanel1= new Panel() ; passPanel1.add(new Label("主角能否通过")) ; passPanel.add(passPanel1,"West") ; String num[] = {"通过","不能通过","状态1","状态2","...",} ; JComboBox choiceBox = new JComboBox(num) ; choiceBox.setEnabled(true); choiceBox.setEditable(true); passPanel.add(choiceBox,"East") ; sysFuncPanel.add(passPanel) ; function.add(sysFuncPanel) ; function.setVisible(true);刚才试了一下,以上的代码确实能实现上面的效果,但是最终界面中,"主角能否通过"这行提示占用了太宽的显示宽度,看起来很不雅观,请问这该怎么办? Panel passPanel = new Panel(new BorderLayout()) ; Panel passPanel1= new Panel() ; passPanel1.add(new Label("主角能否通过")) ; passPanel.add(passPanel1,"West") ; String num[] = {"通过","不能通过","状态1","状态2","...",} ; JComboBox choiceBox = new JComboBox(num) ; choiceBox.setEnabled(true); choiceBox.setEditable(true); passPanel.add(choiceBox,"East") ; sysFuncPanel.add(passPanel) ; function.add(sysFuncPanel) ; function.setVisible(true);刚才试了一下,以上的代码确实能实现上面的效果,但是最终界面中,"主角能否通过"这行提示占用了太宽的显示宽度,看起来很不雅观,请问这该怎么办? 答:GUI布局我最拿手,可就是看不到图!右键点一下显示图片还是没有用啊。有点儿急...,但无奈中.... 这个好办啊,使用嵌套的Border和GridLayout来进行布局有点死板。有个高难度的GridBagLayout。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class GridBagWindow extends JFrame { private JButton searchBtn; private JComboBox modeCombo; private JLabel tagLbl; private JLabel tagModeLbl; private JLabel previewLbl; private JTable resTable; private JTextField tagTxt; public GridBagWindow() { Container contentPane = getContentPane(); GridBagLayout gridbag = new GridBagLayout(); contentPane.setLayout(gridbag); GridBagConstraints c = new GridBagConstraints(); //setting a default constraint value c.fill =GridBagConstraints.HORIZONTAL; tagLbl = new JLabel("Tags"); c.gridx = 0; //x grid position c.gridy = 0; //y grid position gridbag.setConstraints(tagLbl, c); //associate the label with a constraint object contentPane.add(tagLbl); //add it to content pane tagModeLbl = new JLabel("Tag Mode"); c.gridx = 0; c.gridy = 1; gridbag.setConstraints(tagModeLbl, c); contentPane.add(tagModeLbl); tagTxt = new JTextField("plinth"); c.gridx = 1; c.gridy = 0; c.gridwidth = 2; gridbag.setConstraints(tagTxt, c); contentPane.add(tagTxt); String[] options = {"all", "any"}; modeCombo = new JComboBox(options); c.gridx = 1; c.gridy = 1; c.gridwidth = 1; gridbag.setConstraints(modeCombo, c); contentPane.add(modeCombo); searchBtn = new JButton("Search"); c.gridx = 1; c.gridy = 2; gridbag.setConstraints(searchBtn, c); contentPane.add(searchBtn); resTable = new JTable(5,3); c.gridx = 0; c.gridy = 3; c.gridwidth = 3; gridbag.setConstraints(resTable, c); contentPane.add(resTable); previewLbl = new JLabel("Preview goes here"); c.gridx = 0; c.gridy = 4; gridbag.setConstraints(previewLbl, c); contentPane.add(previewLbl); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });} public static void main(String args[]) { GridBagWindow window = new GridBagWindow(); window.setTitle("GridBagWindow"); window.pack(); window.setVisible(true); }} public class TestFrame2 extends JFrame { public TestFrame2(){ super(); this.setTitle("test frame"); this.setSize(500, 500); JLabel title = new JLabel("主角能否通过"); title.setBorder(BorderFactory.createLineBorder(Color.BLACK)); title.setHorizontalAlignment(SwingConstants.CENTER); String[] items = {"1", "2", "3"}; JComboBox list = new JComboBox(items); JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 0.2; gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; panel.add(title, gbc); gbc.weightx = 0.8; gbc.gridx = 1; panel.add(list, gbc); getContentPane().add(panel, BorderLayout.CENTER); } 你可以到这个地址去看http://photo.sina.com.cn/u/1132553125第一张就是 求教装饰模式和代理模式的区别 java读源码的地方 两个程序同时访问mysql的一个数据表,怎么实现? '\r'与'\n'到底有何区别啊?请高手指教! 谁有JDK1.4中文的API文档,谢谢!! 怎么用proguard给class加密 我在做一个GUI查询的时候出现了这样的问题! 请问java中全局变量应该放哪儿? 为什么我用必须在*.class文件路径下,才能用java命令运行*.class? 关于protected的访问问题。 再问内部类中的一个例子 java打开word报错了怎么办啊?
就是说行分成两部分,左边一半是文字说明,右边一般是一个下拉列表,用户可以在右边的下拉列表中选择。BorderLayout我试了,实现这种功能不知道怎么弄。难道把左边添加一个Panel,然后add(new Label("主角能否通过")) ,然后在右边在添加JComboBox
这样来做么?
Panel passPanel = new Panel(new BorderLayout()) ;
Panel passPanel1= new Panel() ;
passPanel1.add(new Label("主角能否通过")) ;
passPanel.add(passPanel1,"West") ;
String num[] = {"通过","不能通过","状态1","状态2","...",} ;
JComboBox choiceBox = new JComboBox(num) ;
choiceBox.setEnabled(true);
choiceBox.setEditable(true);
passPanel.add(choiceBox,"East") ;
sysFuncPanel.add(passPanel) ;
function.add(sysFuncPanel) ;
function.setVisible(true);
刚才试了一下,以上的代码确实能实现上面的效果,但是最终界面中,"主角能否通过"这行提示占用了太宽的显示宽度,看起来很不雅观,请问这该怎么办?
Panel passPanel = new Panel(new BorderLayout()) ;
Panel passPanel1= new Panel() ;
passPanel1.add(new Label("主角能否通过")) ;
passPanel.add(passPanel1,"West") ;
String num[] = {"通过","不能通过","状态1","状态2","...",} ;
JComboBox choiceBox = new JComboBox(num) ;
choiceBox.setEnabled(true);
choiceBox.setEditable(true);
passPanel.add(choiceBox,"East") ;
sysFuncPanel.add(passPanel) ;
function.add(sysFuncPanel) ;
function.setVisible(true);
刚才试了一下,以上的代码确实能实现上面的效果,但是最终界面中,"主角能否通过"这行提示占用了太宽的显示宽度,看起来很不雅观,请问这该怎么办?
这个好办啊,使用嵌套的Border和GridLayout来进行布局有点死板。有个高难度的GridBagLayout。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBagWindow extends JFrame {
private JButton searchBtn;
private JComboBox modeCombo;
private JLabel tagLbl;
private JLabel tagModeLbl;
private JLabel previewLbl;
private JTable resTable;
private JTextField tagTxt;
public GridBagWindow() {
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
contentPane.setLayout(gridbag);
GridBagConstraints c = new GridBagConstraints();
//setting a default constraint value
c.fill =GridBagConstraints.HORIZONTAL;
tagLbl = new JLabel("Tags");
c.gridx = 0; //x grid position
c.gridy = 0; //y grid position
gridbag.setConstraints(tagLbl, c); //associate the label with a constraint object
contentPane.add(tagLbl); //add it to content pane
tagModeLbl = new JLabel("Tag Mode");
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(tagModeLbl, c);
contentPane.add(tagModeLbl);
tagTxt = new JTextField("plinth");
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 2;
gridbag.setConstraints(tagTxt, c);
contentPane.add(tagTxt);
String[] options = {"all", "any"};
modeCombo = new JComboBox(options);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
gridbag.setConstraints(modeCombo, c);
contentPane.add(modeCombo);
searchBtn = new JButton("Search");
c.gridx = 1;
c.gridy = 2;
gridbag.setConstraints(searchBtn, c);
contentPane.add(searchBtn);
resTable = new JTable(5,3);
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 3;
gridbag.setConstraints(resTable, c);
contentPane.add(resTable);
previewLbl = new JLabel("Preview goes here");
c.gridx = 0;
c.gridy = 4;
gridbag.setConstraints(previewLbl, c);
contentPane.add(previewLbl);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
GridBagWindow window = new GridBagWindow();
window.setTitle("GridBagWindow");
window.pack();
window.setVisible(true);
}
}
public class TestFrame2 extends JFrame {
public TestFrame2(){
super();
this.setTitle("test frame");
this.setSize(500, 500);
JLabel title = new JLabel("主角能否通过");
title.setBorder(BorderFactory.createLineBorder(Color.BLACK));
title.setHorizontalAlignment(SwingConstants.CENTER);
String[] items = {"1", "2", "3"};
JComboBox list = new JComboBox(items);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.2;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
panel.add(title, gbc);
gbc.weightx = 0.8;
gbc.gridx = 1;
panel.add(list, gbc);
getContentPane().add(panel, BorderLayout.CENTER);
}
http://photo.sina.com.cn/u/1132553125
第一张就是