paintComponentを持つJPanelをJFrameに追加する際に問題があります。
これがフレームに追加する唯一のものなら、動作します。しかし、レイアウトマネージャを追加してJFrameに他のコンポーネントを追加するとすぐに、ペイントしたパネルが表示されなくなりました!これをより明確にするためにフレームにpaintComponentパネルを追加できません
...
これが機能するとのJPanelが正常に表示されたコードです:
記号(実際に私はハローペイントしようとしていない午前を描くパネルを、この
public class SignFrame extends JFrame {
// the constructor method
public SignFrame() {
super("This is the title of the Sign Frame");
setSize(300,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make a container for the frame
Container content = getContentPane();
// call from the drawing panel
SignPanel2 signpanel = new SignPanel2();
// change class variable of SignPanel
signpanel.hello = 5;
signpanel.repaint();
// add signpanel to container
content.add(signpanel);
setContentPane(content);
setVisible(true);
}
}
:ここでは単にコード)
public class SignPanel2 extends JPanel {
public int hello;
public void paintComponent(Graphics comp) {
Graphics g = (Graphics) comp;
g.setColor(Color.LIGHT_GRAY);
g.fillRect(70, 250, 150, 150);
g.setColor(Color.BLACK);
if (hello > 0)
g.drawString("h",135, 270);
if (hello > 1)
g.drawString("h e",135, 270);
if (hello > 2)
g.drawString("h e l",135, 270);
if (hello > 3)
g.drawString("h e l l",135, 270);
if (hello > 4)
g.drawString("h e l l o",135, 270);
}
}
私は上のパネルを置くフレームにあります
メインクラス
public class TheSignMain {
public static void main (String[] args) {
SignFrame signframe = new SignFrame();
}
}
上記作品完全に罰金、私にそれに所望の描画を持つフレームを提供します。
フレームに他のコンポーネントを追加してレイアウトマネージャを追加しても、もはやそのペイントが表示されません。私がrepaint()を使っていても。
私はレイアウトマネージャを含める必要があります。それ以外の場合、ペイントはパネルに追加されますが、他のコンポーネントは追加されません。 これは私のフレームクラスが今見える方法で、これは私が問題を抱えているところです。
パブリッククラスSignFrameは、私は、Javaに全く新しいですので、任意のヘルプははるかに高く評価されたJFrame {
// the constructor method
public SignFrame() {
super("This is the title of the Sign Frame");
setSize(300,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make a container for the frame
Container content = getContentPane();
// need a layout manager to decide the arrangement of the components on the container
FlowLayout flo = new FlowLayout();
// designate the layout manager to the container
content.setLayout(flo);
// make components
JPanel buttons = new JPanel();
JButton play = new JButton("Play");
JButton pause = new JButton("Pause");
JButton stop = new JButton("Stop");
// add components to a panel
buttons.add(play);
buttons.add(pause);
buttons.add(stop);
// add panel to frame container
content.add(buttons);
// call from the drawing panel
SignPanel2 signpanel = new SignPanel2();
// change class variable of SignPanel
signpanel.hello = 5;
signpanel.repaint();
// add signpanel to container
content.add(signpanel);
setContentPane(content);
setVisible(true);
}
}
を拡張します。 ご迷惑をおかけして申し訳ございません。
+1でも、素敵な質問です。今日何かを学ぶのを助けました:-) –
@Gagandeep ...私はあなたの答えに何をしたのか分かりません。 JB Nizetの横にあるチェックマークをクリックして、それを受け入れられた答えにしました(つまり、それは問題を解決しました)が、あなたの返事は消えました。私はそれが他の回答を削除するとは思わなかった。私はお詫びします、私は今日登録しただけなので、私はフォーラムの仕組みにはまったく新しいものです。 – phy2sma
Nahは慎重だったので、私はそれを削除しました。あなたが望んでいたことと、JB Nizetがあなたに言ったことはとても良いことです。ちょっと笑顔で、心配することはありません:-) –