Java选择布局
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java选择布局,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5783字,纯文字阅读大概需要9分钟。
内容图文
![Java选择布局](/upload/InfoBanner/zyjiaocheng/742/b67468c5cd1f41e7bed91b23fe20cfe2.jpg)
我需要在Java中使用以下布局:
但是,我应该发现没有布局管理器可以简单地为我处理这个问题.我需要在JFrame中使用此布局.
有没有中途简单的方法我能做到这一点?
提前致谢!
编辑:谢谢你们所有人,我终于成功了!
这就是我所做的(正如你所提议的那样)
>窗口的BorderLayout
> leftPanel,GridLayout(1,1)/ * to stretch * /,在窗口中添加Component 1,WEST
> rightPanel,BorderLayout,CENTER在窗口中
> rightTop(添加到rightPanel作为CENTER)面板,添加组件2
> rightBottom(添加到rightPanel作为SOUTH)面板,GridLayout(1,1)(也用于拉伸),添加组件3
谢谢大家,我的建议我混合了^^
解决方法:
我知道你通过放置在BorderLayout布局管理器中的混合JPanel解决了你的问题.如果有效,那没关系.
对于那些对使用GridBagLayout作为解决方案感到好奇的人,我将此类作为示例.只需复制粘贴,编译和运行即可.拖动这个小应用程序窗口的角落,看看调整窗口大小的效果.
// Example code showing the flexibility of GridBagLayout.
// Source code generated by WindowBuilder 1.0.0r37 in Eclipse (Indigo Release) on Mac OS X 10.6.7.
// ? 2011 Basil Bourque. http://www.GridsGoneWild.com/
// This source code may be used freely forever by anyone taking full responsibility for doing so.
// Each layout manager bundled in Java is quite different from the others. They all have strengths and weaknesses,
// each designed for different purposes and effects.
// GridBagLayout is the most powerful and flexible, but takes some patient practice to understand.
// Funny animated video commentary by a GridBagLayout programmer: http://madbean.com/anim/totallygridbag/
// Hand-coding GridBagLayout is certainly tedious, and nearly impossible for complex forms. So I recommend the use of
// a visual GUI builder such as WindowBuilder in Eclipse, JFormDesigner from FormDev.com, or Matisse in NetBeans.
// Key ideas, "grow" & "fill":
// ? In WindowBuilder's Design View, select a widget, then use the "Horizontal grow" and "Vertical grow" icons found in the upper right tool bar.
// ? Use the widget's (label's, button's) "Constraints" > "fill" property in the property sheet of WindowBuilder.
// Further ideas:
// ? To contain multiple widgets in each area, use JPanel objects where I have used single JLabel and JButton objects.
// Nesting JPanels inside JPanels (or in the JFrame's contentPane) is considered normal in Swing. Each JPanel has its own layout manager.
// ? If need be, you can set the Minimum, Maximum, and/or Preferred size of a widget or JPanel.
// ? If you want certain widgets or JPanels to get disproportionately more or less of the space gained or lost when a window is resized,
// use "weightx" and "weighty" properties.
// WindowBuilder Tips:
// ? If it's your first time: Open the .java file, then click the "Design" button at bottom to see visual editor.
// ? Click the "Show Advanced properties" icon at top of a widget's property sheet to see many hidden properties.
// package com.your.package.goes.here; // Uncomment this line, and modify to suit your own package.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Insets;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GridBagLayout_Example extends JFrame {
private JPanel contentPane;
private JLabel lblVariableXVariableY;
private JButton btnVariableXFixedY;
private JLabel lblFixedXVariableY;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GridBagLayout_Example frame = new GridBagLayout_Example();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Constructor
*/
public GridBagLayout_Example() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
this.contentPane = new JPanel();
this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(this.contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
this.contentPane.setLayout(gbl_contentPane);
this.lblFixedXVariableY = new JLabel("Fixed x, Variable y");
this.lblFixedXVariableY.setOpaque(true);
this.lblFixedXVariableY.setBackground(new Color(233, 150, 122));
GridBagConstraints gbc_lblFixedXVariableY = new GridBagConstraints();
gbc_lblFixedXVariableY.fill = GridBagConstraints.VERTICAL;
gbc_lblFixedXVariableY.gridheight = 2;
gbc_lblFixedXVariableY.insets = new Insets(0, 0, 5, 5);
gbc_lblFixedXVariableY.gridx = 0;
gbc_lblFixedXVariableY.gridy = 0;
this.contentPane.add(this.lblFixedXVariableY, gbc_lblFixedXVariableY);
this.lblVariableXVariableY = new JLabel("Variable x & y");
this.lblVariableXVariableY.setBackground(new Color(147, 112, 219));
this.lblVariableXVariableY.setOpaque(true);
GridBagConstraints gbc_lblVariableXVariableY = new GridBagConstraints();
gbc_lblVariableXVariableY.fill = GridBagConstraints.BOTH;
gbc_lblVariableXVariableY.insets = new Insets(0, 0, 5, 0);
gbc_lblVariableXVariableY.gridx = 1;
gbc_lblVariableXVariableY.gridy = 0;
this.contentPane.add(this.lblVariableXVariableY, gbc_lblVariableXVariableY);
this.btnVariableXFixedY = new JButton("Variable x, Fixed y");
this.btnVariableXFixedY.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
this.btnVariableXFixedY.setOpaque(true);
GridBagConstraints gbc_btnVariableXFixedY = new GridBagConstraints();
gbc_btnVariableXFixedY.fill = GridBagConstraints.HORIZONTAL;
gbc_btnVariableXFixedY.gridx = 1;
gbc_btnVariableXFixedY.gridy = 1;
this.contentPane.add(this.btnVariableXFixedY, gbc_btnVariableXFixedY);
}
}
内容总结
以上是互联网集市为您收集整理的Java选择布局全部内容,希望文章能够帮你解决Java选择布局所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。