关于Swing 控件覆盖问题

缸缸 2008-10-28 10:41:41
点+按钮时候上面的文本域会变高并且把下面那个文本域覆盖了,如何做到不覆盖,下面的文本域自动下移

package com.myframe;


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FrameTest extends JFrame{
public FrameTest() {
}
public static void main(String args[]){
SimpleFrame frame = new SimpleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.pack();
frame.setVisible(true);
}
}

class SimpleFrame extends JFrame{
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEITH = 400;
public static final int DEFAULTMAX_HEIGHT = 120;
private JPanel panel;
private JTextArea textArea,textArea1;
private JScrollPane scrollPanel,scrollPanel1;
private JButton bigbutton;
private JToolBar bar;

public SimpleFrame(){
setTitle("框框");
setSize(DEFAULT_WIDTH,DEFAULT_HEITH);

bar = new JToolBar();
bigbutton = new JButton("+");
bigbutton.addActionListener(new AddAction());

bar.add(bigbutton);
add(bar,BorderLayout.NORTH);

panel = new JPanel();
add(panel);
bar.add(bigbutton);

textArea = new JTextArea();
textArea.setRows(5);
textArea.setColumns(20);
textArea.setLineWrap(true);
scrollPanel = new JScrollPane(textArea);
panel.add(scrollPanel);

textArea1 = new JTextArea();
textArea1.setRows(5);
textArea1.setColumns(20);
textArea1.setLineWrap(true);
scrollPanel1 = new JScrollPane(textArea1);
panel.add(scrollPanel1);

JPopupMenu popup = new JPopupMenu();
textArea.setComponentPopupMenu(popup);
JMenuItem item = new JMenuItem("变大");
popup.add(item);
item.addActionListener(new AddAction());
textArea.add(popup);
}

private class AddAction implements ActionListener{
public void actionPerformed(ActionEvent event){
int i= scrollPanel.getHeight();
if(i<DEFAULTMAX_HEIGHT){
scrollPanel.setBounds(
new Rectangle(scrollPanel.getX(), scrollPanel.getY(),
scrollPanel.getWidth(),
scrollPanel.getHeight() + 40));
scrollPanel.updateUI();
}
}
}
}
...全文
495 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chaosking 2008-10-31
  • 打赏
  • 举报
回复
布局的话,要想用好SWING,GridBagLagout是必须要熟悉的。推荐LZ去看下这方面的资料。不过LZ的问题用Grid就可以实现了
horizonlyhw 2008-10-28
  • 打赏
  • 举报
回复
這個用box應該挺合適 你找個小例子做做吧 我不常用布局~ 都是用null~~ 也不熟悉
缸缸 2008-10-28
  • 打赏
  • 举报
回复
我正是想通过布局方式让下面的框框给“挤”下来,可是对布局不是很懂。
horizonlyhw 2008-10-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cowputer 的回复:]
别用null布局,不就省了好多麻烦吗
[/Quote]

嗯 這樣蠻好的~~
cowputer 2008-10-28
  • 打赏
  • 举报
回复
别用null布局,不就省了好多麻烦吗
缸缸 2008-10-28
  • 打赏
  • 举报
回复
谢大虾,你是重新绘制了下面那个文本改变它的位置。但是事实上我的程序里下面还有好几个文本框,这样做就不合适了。
horizonlyhw 2008-10-28
  • 打赏
  • 举报
回复


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FrameTest extends JFrame{
public FrameTest() {
}
public static void main(String args[]){
SimpleFrame frame = new SimpleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.pack();
frame.setVisible(true);
}
}

class SimpleFrame extends JFrame{
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEITH = 400;
public static final int DEFAULTMAX_HEIGHT = 130;
private JPanel panel;
private JTextArea textArea,textArea1;
private JScrollPane scrollPanel,scrollPanel1;
private JButton bigbutton;
private JToolBar bar;

public SimpleFrame(){
setTitle("dfsd");
setSize(DEFAULT_WIDTH,DEFAULT_HEITH);

bar = new JToolBar();
bigbutton = new JButton("+");
bigbutton.addActionListener(new AddAction());

bar.add(bigbutton);
add(bar,BorderLayout.NORTH);

panel = new JPanel();
add(panel);
bar.add(bigbutton);

panel.setLayout(null);

textArea = new JTextArea();
textArea.setRows(5);
textArea.setColumns(20);
textArea.setLineWrap(true);
scrollPanel = new JScrollPane(textArea);
scrollPanel.setBounds(new Rectangle(20,20,100,50));
panel.add(scrollPanel);

textArea1 = new JTextArea();
textArea1.setRows(5);
textArea1.setColumns(20);
textArea1.setLineWrap(true);
scrollPanel1 = new JScrollPane(textArea1);
scrollPanel1.setBounds(new Rectangle(20,100,100,50));
panel.add(scrollPanel1);

JPopupMenu popup = new JPopupMenu();
textArea.setComponentPopupMenu(popup);
JMenuItem item = new JMenuItem("變大");
popup.add(item);
item.addActionListener(new AddAction());
textArea.add(popup);
}

private class AddAction implements ActionListener{
public void actionPerformed(ActionEvent event){
int i= scrollPanel.getHeight();
if(i <DEFAULTMAX_HEIGHT){
scrollPanel.setBounds(
new Rectangle(scrollPanel.getX(), scrollPanel.getY(),
scrollPanel.getWidth(),
scrollPanel.getHeight() + 40));
scrollPanel.updateUI();
if (scrollPanel.getY()+ scrollPanel.getHeight()> scrollPanel1.getY()){
scrollPanel1.setBounds(new Rectangle(scrollPanel1.getX(), scrollPanel.getY()+scrollPanel.getHeight()+20,scrollPanel1.getWidth(),
scrollPanel1.getHeight()));
panel.add(scrollPanel1);
}
scrollPanel1.updateUI();
}
}
}
}

62,635

社区成员

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

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