如何在 Java 中安排组件来右对齐到流?
使用 FlowLayout.RIGHT 安排组件,使组件在 FlowLayout 中右对齐。−
JFrame frame = new JFrame("Language"); frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
以下是一个将组件排列成右对齐流的示例 −
示例
package my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame("Language"); frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); JLabel label = new JLabel("Most Spoken Language "); label.setPreferredSize(new Dimension(220, 70)); label.setOpaque(true); label.setBackground(Color.RED); label.setForeground(Color.WHITE); Font font = new Font("Serif", Font.BOLD, 14); label.setFont(font); label.setHorizontalAlignment(JLabel.CENTER); JTextArea text = new JTextArea(); text.setText("India"); font = new Font("Serif", Font.BOLD, 13); text.setFont(font); frame.add(label); frame.add(text); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(600, 300); frame.setVisible(true); } }
输出
广告