在JPanel里面加LPanel添加文本问题

顾染尘 2020-01-02 05:19:07
package com.SoftWare.serviceComp.video;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class tetest extends JFrame{
public tetest() {
setBounds(100, 100, 691, 511);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel jb2 = new JLabel("456"+"567");

JPanel contentPane1 = new JPanel();
JPanel contentPane2 = new JPanel();
contentPane1.add(jb2);
JLabel jb1 = new JLabel("123"+"567");
contentPane1.add(jb1);
}


}


想让文本JLabel jb2 = new JLabel("456"+"567");和JLabel jb1 = new JLabel("123"+"567");并排,就是实现换行
...全文
380 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39936465 2020-01-03
  • 打赏
  • 举报
回复
引用 6 楼 顾染尘 的回复:
右边的文本框主要起一个提示作用不需要太大[/quote] 大小可以自己调整啊,我只是随意设置了一个大小。
顾染尘 2020-01-03
  • 打赏
  • 举报
回复
引用 4 楼 qq_39936465 的回复:
[quote=引用 1 楼 顾染尘 的回复:]
JLabel jb1 = new JLabel("123"+"567");
				JLabel jb2 = new JLabel("asd");
				panel.add(jb1);
				panel.add(jb2);
在项目中使用,实际只能显示最后一行添加的文本,前面无论加几行,都是显示最后加进去的文本,求解 想让他们并排 这是实际效果图
Jpanel默认布局是flowlayout 想换行显示可以使用 gridlayout, swing设计先搞清楚自己添加的容器是什么布局。

public class test27 {
	
	test27(){
		JFrame jf = new JFrame();
		JPanel jp = new JPanel();
		jp.setLayout(new GridLayout(5, 1));
		JLabel jl1 = new JLabel("1234567");
		JLabel jl2 = new JLabel("76544331");
		jp.add(jl1);
		jp.add(jl2);
		jf.add(jp);
		jf.setVisible(true);
		jf.setMinimumSize(new Dimension(300, 200));
		jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new test27();
	}

}
[/quote] 右边的文本框主要起一个提示作用不需要太大
顾染尘 2020-01-03
  • 打赏
  • 举报
回复
引用 4 楼 qq_39936465的回复:
[quote=引用 1 楼 顾染尘 的回复:]
JLabel jb1 = new JLabel("123"+"567");
				JLabel jb2 = new JLabel("asd");
				panel.add(jb1);
				panel.add(jb2);
在项目中使用,实际只能显示最后一行添加的文本,前面无论加几行,都是显示最后加进去的文本,求解 想让他们并排 这是实际效果图
Jpanel默认布局是flowlayout 想换行显示可以使用 gridlayout, swing设计先搞清楚自己添加的容器是什么布局。

public class test27 {
	
	test27(){
		JFrame jf = new JFrame();
		JPanel jp = new JPanel();
		jp.setLayout(new GridLayout(5, 1));
		JLabel jl1 = new JLabel("1234567");
		JLabel jl2 = new JLabel("76544331");
		jp.add(jl1);
		jp.add(jl2);
		jf.add(jp);
		jf.setVisible(true);
		jf.setMinimumSize(new Dimension(300, 200));
		jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new test27();
	}

}
[/quote] 我也搞到这步了,现在在找有没有什么方式让第一列和第二列的宽度打到3比1
qq_39936465 2020-01-03
  • 打赏
  • 举报
回复
引用 1 楼 顾染尘 的回复:
JLabel jb1 = new JLabel("123"+"567");
				JLabel jb2 = new JLabel("asd");
				panel.add(jb1);
				panel.add(jb2);
在项目中使用,实际只能显示最后一行添加的文本,前面无论加几行,都是显示最后加进去的文本,求解 想让他们并排 这是实际效果图
Jpanel默认布局是flowlayout 想换行显示可以使用 gridlayout, swing设计先搞清楚自己添加的容器是什么布局。

public class test27 {
	
	test27(){
		JFrame jf = new JFrame();
		JPanel jp = new JPanel();
		jp.setLayout(new GridLayout(5, 1));
		JLabel jl1 = new JLabel("1234567");
		JLabel jl2 = new JLabel("76544331");
		jp.add(jl1);
		jp.add(jl2);
		jf.add(jp);
		jf.setVisible(true);
		jf.setMinimumSize(new Dimension(300, 200));
		jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new test27();
	}

}
sunyiz 2020-01-03
  • 打赏
  • 举报
回复
方法有两种
1、JLabel 的文字支持 html 格式,setText("<html>" + 内容1 + "<br>" + 内容2 + ……);
<html> 开头就行,<br>是换行

2、给JPanel设定一个垂直版的流布局,我给你一个写好的垂直流布局管理器

public class VerticalFlowLayout extends FlowLayout {

public static final int TOP = 0;
public static final int MIDDLE = 1;
public static final int BOTTOM = 2;

int hgap;
int vgap;
boolean hfill;
boolean vfill;

/**
* Construct a new VerticalFlowLayout with a middle alignemnt, and the fill
* to edge flag set.
*/
public VerticalFlowLayout() {
this(TOP, 5, 5, true, false);
}

/**
* Construct a new VerticalFlowLayout with a middle alignemnt.
*
* @param fill
* the fill to edge flag
*/
public VerticalFlowLayout(boolean hfill, boolean vfill) {
this(TOP, 5, 5, hfill, vfill);
}

/**
* Construct a new VerticalFlowLayout with a middle alignemnt.
*
* @param align
* the alignment value
*/
public VerticalFlowLayout(int align) {
this(align, 5, 5, true, false);
}

public VerticalFlowLayout(int align, int hgap, int vgap) {
this(align, hgap, vgap, true, false);
}

/**
* Construct a new VerticalFlowLayout.
*
* @param align
* the alignment value
* @param fill
* the fill to edge flag
*/
public VerticalFlowLayout(int align, boolean hfill, boolean vfill) {
this(align, 5, 5, hfill, vfill);
}

/**
* Construct a new VerticalFlowLayout.
*
* @param align
* the alignment value
* @param hgap
* the horizontal gap variable
* @param vgap
* the vertical gap variable
* @param fill
* the fill to edge flag
*/
public VerticalFlowLayout(int align, int hgap, int vgap, boolean hfill,
boolean vfill) {
setAlignment(align);
this.hgap = hgap;
this.vgap = vgap;
this.hfill = hfill;
this.vfill = vfill;
}

/**
* Gets the horizontal gap between components.
*
* @return the horizontal gap between components.
* @see java.awt.FlowLayout#setHgap
* @since JDK1.1
*/
@Override
public int getHgap() {
return hgap;
}

/**
* Sets the horizontal gap between components.
*/
@Override
public void setHgap(int hgap) {
super.setHgap(hgap);
this.hgap = hgap;
}

/**
* Gets the vertical gap between components.
*/
@Override
public int getVgap() {
return vgap;
}

/**
* Sets the vertical gap between components.
*/
@Override
public void setVgap(int vgap) {
super.setVgap(vgap);
this.vgap = vgap;
}

/**
* Returns the preferred dimensions given the components in the target
* container.
*
* @param target
* the component to lay out
*/
@Override
public Dimension preferredLayoutSize(Container target) {
Dimension tarsiz = new Dimension(0, 0);

for (int i = 0; i < target.getComponentCount(); i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
tarsiz.width = Math.max(tarsiz.width, d.width);
if (i > 0) {
tarsiz.height += vgap;
}
tarsiz.height += d.height;
}
}
Insets insets = target.getInsets();
tarsiz.width += insets.left + insets.right + hgap * 2;
tarsiz.height += insets.top + insets.bottom + vgap * 2;
return tarsiz;
}

/**
* Returns the minimum size needed to layout the target container
*
* @param target
* the component to lay out
*/
@Override
public Dimension minimumLayoutSize(Container target) {
Dimension tarsiz = new Dimension(0, 0);

for (int i = 0; i < target.getComponentCount(); i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getMinimumSize();
tarsiz.width = Math.max(tarsiz.width, d.width);
if (i > 0) {
tarsiz.height += vgap;
}
tarsiz.height += d.height;
}
}
Insets insets = target.getInsets();
tarsiz.width += insets.left + insets.right + hgap * 2;
tarsiz.height += insets.top + insets.bottom + vgap * 2;
return tarsiz;
}

public void setVerticalFill(boolean vfill) {
this.vfill = vfill;
}

public boolean getVerticalFill() {
return vfill;
}

public void setHorizontalFill(boolean hfill) {
this.hfill = hfill;
}

public boolean getHorizontalFill() {
return hfill;
}

/**
* places the components defined by first to last within the target
* container using the bounds box defined
*
* @param target
* the container
* @param x
* the x coordinate of the area
* @param y
* the y coordinate of the area
* @param width
* the width of the area
* @param height
* the height of the area
* @param first
* the first component of the container to place
* @param last
* the last component of the container to place
*/
private void placethem(Container target, int x, int y, int width,
int height, int first, int last) {
int align = getAlignment();
// if ( align == this.TOP )
// y = 0;
if (align == MIDDLE)
y += height / 2;
if (align == BOTTOM)
y += height;

for (int i = first; i < last; i++) {
Component m = target.getComponent(i);
Dimension md = m.getSize();
if (m.isVisible()) {
int px = x + (width - md.width) / 2;
m.setLocation(px, y);
y += vgap + md.height;
}
}
}

/**
* Lays out the container.
*
* @param target
* the container to lay out.
*/
@Override
public void layoutContainer(Container target) {
Insets insets = target.getInsets();
int maxheight = target.getSize().height
- (insets.top + insets.bottom + vgap * 2);
int maxwidth = target.getSize().width
- (insets.left + insets.right + hgap * 2);
int numcomp = target.getComponentCount();
int x = insets.left + hgap;
int y = 0;
int colw = 0, start = 0;

for (int i = 0; i < numcomp; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
// fit last component to remaining height
if ((this.vfill) && (i == (numcomp - 1))) {
d.height = Math.max((maxheight - y),
m.getPreferredSize().height);
}

// fit componenent size to container width
if (this.hfill) {
m.setSize(maxwidth, d.height);
d.width = maxwidth;
} else {
m.setSize(d.width, d.height);
}

if (y + d.height > maxheight) {
placethem(target, x, insets.top + vgap, colw,
maxheight - y, start, i);
y = d.height;
x += hgap + colw;
colw = d.width;
start = i;
} else {
if (y > 0)
y += vgap;
y += d.height;
colw = Math.max(colw, d.width);
}
}
}
placethem(target, x, insets.top + vgap, colw, maxheight - y, start, numcomp);
}
}
zllstyle 2020-01-02
  • 打赏
  • 举报
回复
加个空布局,自己调位置 面板.setLayout(null);
顾染尘 2020-01-02
  • 打赏
  • 举报
回复
JLabel jb1 = new JLabel("123"+"567");
				JLabel jb2 = new JLabel("asd");
				panel.add(jb1);
				panel.add(jb2);
在项目中使用,实际只能显示最后一行添加的文本,前面无论加几行,都是显示最后加进去的文本,求解 想让他们并排 这是实际效果图

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧