java java gui swing

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java java gui swing相关的知识,希望对你有一定的参考价值。

import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

/**
 * Example code for lecture 9.
 * @author Jeffrey Eppinger & Terry Lee
 */
public class QuoteGUI2 {

    /**
     * Constructor where JFrame and other components are instantiated.
     */
    public QuoteGUI2() {
        JFrame frame = new JFrame("08-671 QuoteGUI Example");
        frame.setSize(520, 420);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel pane = new JPanel();

        Font bigFont = new Font(Font.SERIF, Font.BOLD, 20);

        JLabel label = new JLabel("How about a presidential quote?");
        label.setFont(bigFont);
        pane.add(label);

        JPanel line = new JPanel();

        ImageIcon obamaIcon = new ImageIcon("obama.jpg");
        JButton obamaButton = new JButton("Barack Obama", obamaIcon);
        obamaButton.setHorizontalTextPosition(SwingConstants.CENTER);
        obamaButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        line.add(obamaButton);

        ImageIcon trumpIcon = new ImageIcon("trump.jpg");
        JButton trumpButton = new JButton("Donald Trump", trumpIcon);
        trumpButton.setHorizontalTextPosition(SwingConstants.CENTER);
        trumpButton.setVerticalTextPosition(SwingConstants.BOTTOM);
        line.add(trumpButton);

        pane.add(line);

        JTextArea area = new JTextArea(10, 40);
        area.setText("Yes, we can.");
        pane.add(area);

        frame.setContentPane(pane);
        frame.setVisible(true);
    }

    /**
     * Simple program to show cookbook programming for Swing.
     * @param args arguments
     */
    public static void main(String[] args) {
        new QuoteGUI2();
    }

}

以上是关于java java gui swing的主要内容,如果未能解决你的问题,请参考以下文章

Swing 是一个为Java设计的GUI工具包

java GUI编程(swing)之二swing按钮组件

java GUI编程(swing)之一 swing简单介绍

用于 GUI 构建的 Java AWT 或 Swing?

由java中的另一个线程刷新GUI(swing)

java GUI编程(swing)之八swing绘图