gpt4 book ai didi

java - 如何强制 JContainer 重新布局自己?

转载 作者:搜寻专家 更新时间:2023-11-01 03:07:59 24 4
gpt4 key购买 nike

我有 JFrameBorderLayout: NORTHJButton 中的 JTextArea。我在开头pack()它。

我的代码更改了文本区域的字体大小。如何强制对话框窗口及其组件重新布局?

到目前为止,我尝试了一些组合:

  • 另一个pack()
  • 重绘()
  • 重新验证()

这似乎没有帮助。

是否有保证的蛮力方法?达到这样的结果的正确方法是什么?

更新:

在创建 SCCE(见下文)时,我在我的原始代码中发现了两个错误并修复了它们。 框架现在可以很好地调整大小。

我仍然有疑问这是否是正确的方法。

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

public class MyFrame extends JFrame implements ActionListener{

private JTextArea txt;
private JButton bis;
private JFrame frame;

int size = 10;

private void BuildMainGUI() {
txt = new JTextArea("This is just a line of text");
bis = new JButton("Increase size");

JPanel p1 = new JPanel();
bis.addActionListener(this);

BorderLayout bl = new BorderLayout();

p1.setLayout(bl);
p1.add(txt, BorderLayout.NORTH);
p1.add(bis, BorderLayout.SOUTH);

frame = new JFrame();
frame.setContentPane(p1);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e) {

size += 2;
Font newFont = new Font("Courier", Font.PLAIN, size);
txt.setFont(newFont);
frame.revalidate();
frame.pack();
}

/**
* @param args
*/
public static void main(String[] args) {

MyFrame myGUI = new MyFrame();
myGUI.BuildMainGUI();
}
}

最佳答案

所有三个备选方案都在 ActionListener 中进行了描述,让我们以最简单的方式工作,似乎坐标是相同的(我认为需要深入研究 TextLayout ???)

enter image description here enter image description here

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class ResizeJTextArea {

private JFrame frame = new JFrame();
private JScrollPane scrollPane = new JScrollPane();
private JTextArea textArea = new JTextArea(10, 15);
private JButton button = new JButton("change");
private Font newFont = new Font("Courier", Font.PLAIN, 10);

public ResizeJTextArea() {
textArea.setText("This is just a line of text");
textArea.setFont(newFont);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.setFont(textArea.getFont().deriveFont(20f));

//2. choice
//textArea.setColumns(20);
//textArea.setRows(20);

//3rd. coice
//override PreferredScrollableViewportSize
frame.pack();
}
});
scrollPane.setViewportView(textArea);
frame.add(scrollPane);
frame.add(button, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ResizeJTextArea fs = new ResizeJTextArea();
}
});
}
}

关于java - 如何强制 JContainer 重新布局自己?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16090941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com