簡體   English   中英

如何強制JContainer重新布局?

[英]How to force JContainer to re-layout itself?

JFrameBorderLayoutJTextAreaNORTHJButtonSOUTH 我在開頭pack()

我的代碼更改了文本區域的字體大小 如何強制對話框窗口及其組件重新布局?

到目前為止,我嘗試了一些組合:

  • 另一pack()
  • repaint()
  • revalidate()

它似乎沒有幫助。

是否有保證蠻力的方法? 實現這種結果的正確方法是什么?

更新:

在創建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 ???)

在此輸入圖像描述在此輸入圖像描述

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();
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM