dポイントプレゼントキャンペーン実施中!

現在、ボタンを押すと

「てすと」(画像ファイル名はtest.jpg)→「てすと2」(画像ファイル名はtest2.jpg)

と画面が切り替えられるプログラムを作成していますが、不明な点があり質問してみました。
2つの画面を作成してみましたが、この後どう合体すればいいのかよく分かりません。分かる方は是非教えて下さい。

<プログラムソース>
(1)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Title extends JFrame{

public Title() {
add(new DrawPanel());
JPanel L = new DrawPanel();
L.setLayout(new BorderLayout());
JPanel L1 = new JPanel();
L1.setOpaque(false);
L1.add(new JButton("OK"));
L.add(L1, BorderLayout.SOUTH);

setContentPane(L);
}

public static void main(String args[]){
JFrame frame = new Title();
frame.setSize(640, 480);
frame.setTitle("てすと");
frame.setLocationRelativeTo(null);
frame.setBackground(Color.pink);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);}
}
class DrawPanel extends JPanel{
String path = "test.jpg";
Image image;
public DrawPanel() {

ImageIcon icon = new ImageIcon(path);
}

public void paintComponent(Graphics args) {
super.paintComponent(args);
args.drawImage(image, 0, 0, this);
args.setFont(new Font("TimesRoman",Font.ITALIC,100));
args.setColor(Color.red);
args.drawString("てすと", 120, 230);
}
}

(2)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Title2 extends JFrame {
public Title2() {
add(new DrawPanel());
JPanel L = new DrawPanel();
L.setLayout(new BorderLayout());
JPanel L1 = new JPanel();
L1.setOpaque(false);
L1.add(new JButton("OK"));
L.add(L1, BorderLayout.SOUTH);

setContentPane(L);
}

public static void main(String args[]){
JFrame frame = new Title2();
frame.setSize(640, 480);
frame.setTitle("てすと");
frame.setLocationRelativeTo(null);
frame.setBackground(Color.white);
frame.setVisible(true);

}
}

class DrawPanel extends JPanel {
String path = "test2.jpg";
Image image;
public DrawPanel() {
ImageIcon icon = new ImageIcon(path);
image = icon.getImage();
}

public void paintComponent(Graphics args) {
super.paintComponent(args);
 args.setFont(new Font("TimesRoman",Font.BOLD,40));
args.setColor(Color.blue);
args.drawString("てすと2", 150, 390);

}
}

A 回答 (2件)

こんな感じですか


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Title extends JFrame implements ActionListener {

boolean inAnApplet = true;

JPanel cards;
JButton kirikae;

final static String TEST_PANEL = "てすと";
final static String TEST2_PANEL = "てすと2";
String currentPanel = TEST_PANEL;

public Title() {
Container contentPane = getContentPane();

kirikae = new JButton("切替");
kirikae.setActionCommand("change");
kirikae.addActionListener(this);
JPanel bp = new JPanel();

bp.add(kirikae);

// Use the default layout manager, BorderLayout
contentPane.add(bp, BorderLayout.SOUTH);

cards = new JPanel();
cards.setLayout(new CardLayout() );

DrawPanel p1 = new DrawPanel ("テストプログラム" , "今回は、CardLayoutを使用して"
, "画像表示のプログラムを作ってみました", "test.jpg" );

DrawPanel p2 = new DrawPanel ("どうでした?" , "皆さんも一度画像表示のプログラムを"
, "作ってみてはいかがでしょうか?", "test2.jpg") ;

cards.add(p1, TEST_PANEL);
cards.add(p2, TEST2_PANEL);
contentPane.add(cards, BorderLayout.CENTER);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
} );
}


public void actionPerformed (ActionEvent e) {
CardLayout cl = (CardLayout)(cards.getLayout() );
if (e.getActionCommand().equals("change")) {
if (currentPanel == TEST_PANEL) {
cl.show(cards, TEST2_PANEL );

currentPanel = TEST2_PANEL;
} else {
cl.show(cards, TEST_PANEL );

currentPanel = TEST_PANEL;
}
}
}

public static void main(String[] argv) {
Title window = new Title();

window.inAnApplet = false;

window.setTitle("CardLayout");
window.pack();
window.setVisible(true);
}

public Dimension getMinimumSize() {
return new Dimension(640,480);
}
public Dimension getPreferredSize() {
return getMinimumSize();
}

}

class DrawPanel extends JPanel{

String message1, message2, message3 ;
String path ;
Image image;

public DrawPanel(String m1, String m2, String m3, String fileName) {

//ImageIcon icon = new ImageIcon(path);

Toolkit toolkit = Toolkit.getDefaultToolkit();
this.path = fileName;
this.message1 = m1;
this.message2 = m2;
this.message3 = m3;
image = toolkit.getImage (path);

}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
g.setFont(new Font("TimesRoman",Font.ITALIC,30));
g.setColor(Color.red);
g.drawString(this.message1 , 60, 230);
g.drawString(this.message2 , 60, 260);
g.drawString(this.message3 , 60, 290);
}

public Dimension getMinimumSize() {
return new Dimension(640,400);
}
public Dimension getPreferredSize() {
return getMinimumSize();
}

}
    • good
    • 0
この回答へのお礼

実行してみましたら、ちゃんと画像と文字が同時に表示しました!
とても助かりましたよ。
本当に有難うございます。

お礼日時:2009/12/08 11:53

ボタンを押すごとに現在表示しているパネルをremove、もう一方のパネルをaddするという方法もありますが、


カードレイアウトを使ってみました。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Title extends JFrame implements ItemListener {

boolean inAnApplet = true;

JPanel cards;
final static String TEST_PANEL = "てすと";
final static String TEST2_PANEL = "てすと2";

public Title() {
Container contentPane = getContentPane();

// Put the JComboBox in a JPanel to get a nicer look.
String comboBoxItems[] = {TEST_PANEL, TEST2_PANEL };
JPanel cbp= new JPanel();
JComboBox c = new JComboBox(comboBoxItems);
c.setEditable(false);
c.addItemListener(this);
cbp.add(c);

// Use the default layout manager, BorderLayout
contentPane.add(cbp, BorderLayout.NORTH);

cards = new JPanel();
cards.setLayout(new CardLayout() );

DrawPanel p1 = new DrawPanel ("てすと", "test.jpg" );

DrawPanel p2 = new DrawPanel ("てすと2", "test2.jpg") ;

cards.add(p1, TEST_PANEL);
cards.add(p2, TEST2_PANEL);
contentPane.add(cards, BorderLayout.CENTER);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
} );
}

public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout() );
cl.show(cards, (String)evt.getItem() );
}

public static void main(String[] argv) {
Title window = new Title();

window.inAnApplet = false;

window.setTitle("CardLayout");
window.pack();
window.setVisible(true);
}

public Dimension getMinimumSize() {
return new Dimension(640,480);
}
public Dimension getPreferredSize() {
return getMinimumSize();
}

}

class DrawPanel extends JPanel{

String titleName;
String path ;
Image image;

public DrawPanel(String title, String fileName) {

//ImageIcon icon = new ImageIcon(path);

Toolkit toolkit = Toolkit.getDefaultToolkit();
this.path = fileName;
this.titleName = title;
image = toolkit.getImage (path);

}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
g.setFont(new Font("TimesRoman",Font.ITALIC,100));
g.setColor(Color.red);
g.drawString(titleName, 120, 230);
}

public Dimension getMinimumSize() {
return new Dimension(640,400);
}
public Dimension getPreferredSize() {
return getMinimumSize();
}

}

この回答への補足

実行してみましたら、ちゃんと画面が映りました。

補足しますけど、今度は「てすと」では・・・
「テストプログラム」
「今回は、CardLayoutを使用して」
「画像表示のプログラムを作ってみました」

「てすと2」では・・・
「どうでした?」
「皆さんも1度画像表示のプログラムを」
「作ってみてはいかがでしょうか?
と各3行文章を表示させたいですが、どうすればいいですか?

あとはJComboboxを使用せず、CardLayoutとボタン1つで画面が切り替わるソースが分かれば、一緒に教えて頂けると有り難いですが。

補足日時:2009/12/07 12:07
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!