网站首页 > 文章中心 > 其它

关于java布局的代码

作者:小编 更新时间:2023-10-18 18:45:19 浏览量:79人看过

java嵌套布局代码

java中网格包布局代码哪里出错?

写的时候仔细点,setLyaout方法里面有错

lbUser=new JLabel("用户名");

gbLayout.setConstraints(tfUser,constraints);

关于java布局的代码-图1

container.add(tfUser);

你这里把 lbUser 指向了对象,此是的tfUser还没有,而你加的时候确是加的tfUser, 你把 tfUser改过来就好了

import javax.swing.*;

import java.awt.*;

public class GridBagLayoutDemo extends JFrame {

private GridBagLayout gbLayout = new GridBagLayout();

private GridBagConstraints constraints = new GridBagConstraints();

private JLabel lbUser, lbPassword;

private JTextField tfUser, tfPassword;

关于java布局的代码-图2

private JButton btnLog;

private Container container;

public GridBagLayoutDemo() {

super("网格包布局");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private void setConstraints(GridBagConstraints gbc, int row, int column,

int numRows, int numColumns, int Weightx, int Weighty) {

关于java布局的代码-图3

gbc.gridx = row;

gbc.gridy = column;

gbc.gridwidth = numRows;

gbc.gridheight = numColumns;

gbc.weightx = Weightx;

gbc.weighty = Weighty;

public void setLyaout() {

container = this.getContentPane();

container.setLayout(gbLayout);

// 添加用户名标签

constraints.fill = GridBagConstraints.NONE;

constraints.anchor = GridBagConstraints.CENTER;

setConstraints(constraints, 0, 0, 1, 1, 0, 0);

lbUser = new JLabel("用户名");

gbLayout.setConstraints(lbUser, constraints);

container.add(lbUser);

// 添加用户名文本框

constraints.fill = GridBagConstraints.HORIZONTAL;

setConstraints(constraints, 1, 0, 1, 1, 100, 100);

tfUser = new JTextField();

gbLayout.setConstraints(tfUser, constraints);

// 添加密码标签

setConstraints(constraints, 0, 1, 1, 1, 0, 0);

lbPassword = new JLabel("密码");

gbLayout.setConstraints(lbPassword, constraints);

container.add(lbPassword);

// 添加密码文本框

setConstraints(constraints, 1, 1, 1, 1, 100, 100);

tfPassword = new JTextField();

gbLayout.setConstraints(tfPassword, constraints);

container.add(tfPassword);

// 添加登录按钮

constraints.fill = GridBagConstraints.CENTER;

btnLog = new JButton("登录");

gbLayout.setConstraints(btnLog, constraints);

container.add(btnLog);

public static void main(String[] args) {

GridBagLayoutDemo frame = new GridBagLayoutDemo();

frame.setLyaout();

frame.show();

java 布局与日历 第一部分

出现:"Appletviewer"不是内部或外部命令

是因为没有配置运行java的环境.

配置java运行环境:

变量名 变量值

CLASSPATH .;%JAVA_HOME%\lib

path %JAVA_HOME%\bin;

[注意:第三个变量path原先已存在值,不要覆盖,只要在前面将 %JAVA_HOME%\bin; 添加到前面就可以了]

怎么用java代码写一个线性布局;布局里面有两个按钮是水平的

android 使两个按钮水平排列的方法是使用lineLayout线性布局,如下代码:

LinearLayout?xmlns:android=""

android:layout_width="fill_parent"

android:layout_height="match_parent"

android:background="@color/background"

android:orientation="vertical"?

View

android:layout_width="wrap_content"

android:background="@color/white"?/

LinearLayout

android:orientation="horizontal"

android:layout_margin="10dp"?

Button

android:id="@+id/bt1"

android:background="@drawable/shape"

android:layout_weight="1"

android:text="确认对冲"

android:textColor="@color/white"

android:background="@drawable/shapeyuanjiao"

android:text="取消"

/LinearLayout

运行结果如下:

java swing布局

null layout不是很好,所有的东西都要自己定义,一旦窗口大小变化就要重新计算.我建议使用MigLayout.上手慢,但很强大.

而且,就算你要用内建的Layout Manager,还有一个GridBag Layout,比Grid Layout更强大一点.

此时此刻呢,那个边框,不是JSeparator, 而是一个边框.我猜是

Border result = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "修改信息: ");

如果是我,使用MigLayout,以上代码还是比较简单的:

import?java.awt.event.KeyEvent;

import?javax.swing.BorderFactory;

import?javax.swing.JButton;

import?javax.swing.JFrame;

import?javax.swing.JLabel;

import?javax.swing.JPanel;

import?javax.swing.JTextField;

import?javax.swing.SwingUtilities;

import?javax.swing.border.Border;

import?javax.swing.border.EtchedBorder;

import?net.miginfocom.swing.MigLayout;

public?class?MyFrame?extends?JFrame?{

public?MyFrame()?{

begin();

private?void?begin()?{

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//first?panel

JPanel?first?=?new?JPanel();

first.setOpaque(false);

//这个面板的border有些特殊:createTitledBorder()方法的签名可以有两个:前一个是线的类型,后面一个是标题文本.

Border?result?=?BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),?"修改信息:?");

first.setBorder(result);

//把first加到contentPane里面

add(first,?"cell?0?0,?grow");

JLabel?original?=?new?JLabel("输入原密码:?");

JLabel?newPass?=?new?JLabel("输入新密码:?");

JLabel?confirm?=?new?JLabel("确认新密码:?");

JTextField?orig_field?=?new?JTextField();

JTextField?new_field?=?new?JTextField();

JTextField?confirm_field?=?new?JTextField();

//miglayout的核心就是网格排布.用坐标来定义添加元素的位置

first.add(original,?"cell?0?0,?w?100!");?//add?to?col?0,?line?0,?min:pref:max?width?all?set?to?100

first.add(newPass,?"cell?0?1,?w?100!");??//add?to?col?0,?line?1

//按钮面板

JPanel?buttons?=?new?JPanel();

buttons.setOpaque(false);

JButton?yes?=?new?JButton("Y.?确定");

//快捷键设为虚拟键Y,得到下划线效果

yes.setMnemonic(KeyEvent.VK_Y);

JButton?quit?=?new?JButton("Q.?退出");

//快捷键设为虚拟键Q,得到下划线效果

quit.setMnemonic(KeyEvent.VK_Q);

//把yes按钮加到第一列第一行,min:pref:max的大小都设为100像素

buttons.add(yes,?"cell?0?0,?w?100!");

//把quit按钮加到第二列第一行,min:pref:max的大小都设为100像素

buttons.add(quit,?"cell?1?0,?w?100!");

//把按钮面板加到contentPane里面

add(buttons,?"cell?0?1,?grow");

pack();

setLocationRelativeTo(null);

setVisible(true);

public?static?void?main(String[]?args)?{

SwingUtilities.invokeLater(new?Runnable()?{

public?void?run()?{

MyFrame?frame?=?new?MyFrame();

});

效果是这样:

MigLayout还允许你使用辅助线来debug.把这一行:

改成:

你就可以看到first面板里面的辅助线了.

java东西南北中布局代码

FlowLayout 流式布局,从左到右,如果到边界就换行再从左到右.

BorderLayout 边界布局(默认布局方式),按东西南北中五个方向来布局,默认是中.后设置在同样位置的控件会覆盖之前的控件.

GridLayout 网格布局,将容器划分成若干行列的网格,从左到右,然后从上到下.每个控件的大小相同.

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章