首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在图形用户界面中使用GridLayout居中显示文本

如何在图形用户界面中使用GridLayout居中显示文本
EN

Stack Overflow用户
提问于 2016-09-10 08:36:07
回答 1查看 489关注 0票数 -1

我需要帮助创建GUI (完全新手:-( ..)

这是用GridLayout创建的,但现在我希望左侧的文本居中显示在TextArea的中间。有没有可能不一直使用"\n“?

代码:

public class guiFrame {
    JLabel label;
    JMenuBar menubar;
    JTextArea area;

    public guiFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(600,200));
        frame.getContentPane().setBackground(Color.BLACK);

        JPanel panel = new JPanel(new BorderLayout());
        panel.setLayout(new GridLayout(1, 2));
        frame.add(panel);

        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);
        JMenu aenderFarb = new JMenu("Ändere Farbe");
        menubar.add(aenderFarb);
        JMenuItem blak = new JMenuItem("schwarz");
        JMenuItem whit = new JMenuItem("weiß");
        aenderFarb.add(blak);
        aenderFarb.add(whit);

        JTextArea area = new JTextArea("Hallo, Welt! Hier kann man Text reinschreiben...");
        panel.add(area);
        panel.setBackground(Color.BLACK);

        JLabel label = new JLabel("");
        label.setBackground(Color.BLACK);
        panel.add(label);
        frame.setVisible(true);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-10 14:25:56

这篇文章的目的是回答你的问题,但也演示了MCVE在未来的问题中的使用。

有关说明,请参阅注释:

//include imports to make code MCVE
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

//use right java naming convention
public class GuiFrame {
    JLabel label;
    JMenuBar menubar;
    JTextArea area;

    public GuiFrame() throws BadLocationException {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(600,200));
        frame.getContentPane().setBackground(Color.BLACK);

        //no point in assigning BorderLayout which is not used
        //JPanel panel = new JPanel(new BorderLayout());
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 2));
        panel.setBackground(Color.BLACK);
        frame.add(panel);

        //remove what is not essential for the question
        //to make code and MCVE
        //JMenuBar menubar = new JMenuBar();


        //to set horizontal alignment you need to use a JTextpane
        //JTextArea area = new JTextArea("Hallo, Welt! Hier kann man Text reinschreiben...");
        String text = "Hallo, Welt! Hier kann man Text reinschreiben...";
        StyleContext context = new StyleContext();
        StyledDocument document = new DefaultStyledDocument(context);

        Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
        StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);

        document.insertString(document.getLength(), text, style);
        JTextPane area = new JTextPane(document);
        panel.add(area);

        //vertical alignment is not supported.
        //see possible solutions here: http:
        //stackoverflow.com/questions/29148464/align-jtextarea-bottom

        //remove what is not essential for the question
        //to make code and MCVE
        //JMenuBar menubar = new JMenuBar();
        //JLabel label = new JLabel("");

        frame.setVisible(true);
    }

    //include a main to make code an MCVE
    public static void main(String[] args) {

        try {
            new GuiFrame();
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39421398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档