0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Javaで「ラベルのサイズの設定と枠線の表示」の動作を確認してみた。

Posted at

概要

Javaで「ラベルのサイズの設定と枠線の表示」の動作を確認してみました。
以下のページを参考にしました。

実装

以下のファイルを作成しました。

Sample.java
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Dimension;

class Sample extends JFrame{
  public static void main(String args[]){
    Sample frame = new Sample("MyTitle");
    frame.setVisible(true);
  }

  Sample(String title){
    setTitle(title);
    setBounds(100, 100, 728, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label1 = new JLabel("Hello!");
    label1.setOpaque(true);
    label1.setBackground(Color.YELLOW);

    JLabel label2 = new JLabel("Good!");
    label2.setOpaque(true);
    label2.setBackground(Color.GREEN);
    label2.setPreferredSize(new Dimension(100, 100));

    JLabel label3 = new JLabel("Bye!");
    label3.setOpaque(true);
    label3.setBackground(Color.RED);
    label3.setPreferredSize(new Dimension(200, 200));

    JPanel p = new JPanel();
    p.add(label1);
    p.add(label2);
    p.add(label3);

    add(p, BorderLayout.CENTER);
  }
}

以下のコマンドを実行しました。

$ javac Sample.java 
$ java Sampl

実行結果は以下の通りでした。

Screenshot from 2025-01-13 07-37-02.png

まとめ

何かの役に立てばと。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?