プログラムのアイデアは、フレームのどこかにいくつかのボタンとアイコンがあるということです。ボタンの色を変えたい。私は今、すべての要素を表示することだけを心配しています。11 行目から 13 行目をコメント アウトすると、"hello" が印刷され、その上に赤い丸が表示されます。それ以外の場合は、「こんにちは」または赤い円のないボタン「赤」だけがあります。だからここに私のコードがあります:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class ButtonTester
{
public static void main (String[] args)
{
JFrame frame = new ButtonFrame();
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton redButton = new JButton("Red");
frame.add(redButton);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ButtonFrame extends JFrame
{
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
public ButtonFrame()
{
setTitle("Hello");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
}
class ButtonPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawString("Hello !", 100, 100);
Icon ico = new ColorIcon(32);
ico.paintIcon(null, g, 75, 75);
}
}
問題は 11 ~ 13 行目であると 90% 確信していますが、すべてを表示するには何を変更すればよいかわかりません。