CardLayoutに関する質問です
前回、swingに関する質問をさせていただいたものです。
前回頂いたご回答をヒントに新しく作り直したのですが、CardLayoutのパネルにFlowLayoutのパネルを追加してもボタンが右端で折り返してくれません。
色々調べたのですが、この二つのレイアウトを使った例が見つかりませんでした。
長文になりますが、以下にソースを記載いたします。
今一度、お知恵をお借りできれば幸いです。
------------以下ソースコード-------------------
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import javax.swing.Timer;
class Main extends JPanel implements ActionListener{
static JFrame frame = new JFrame();
String[] imgStr = { "1","2","3","4","5","6","7","8","9",
"10","11","12","13","14","15","16","17","18",
"19","20","21","22","23","24","25","26","27",
"28","29","30","31","32","33","34","35","36",
"37","38","39","40","41","42","43","44","45","46","終了"};
JButton[] bt = new JButton[imgStr.length];
JPanel mainPanel = new JPanel();
JPanel btPanel = new JPanel();
JPanel image;
MediaTracker tracker;
CardLayout cardLayout = new CardLayout();
Image img;
ImageCanvas canvas;
Timer time = new Timer(0,this);
int eventNo = -1;
public static void main(String[] args){
frame.add(new Main());
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 1024, 576);
frame.setTitle("タイトル");
frame.setVisible(true);
}
public Main(){
mainPanel.setLayout(cardLayout);
mainPanel.setBounds(0, 0, 1024, 576);
btPanel.setBounds(0, 0, 1024, 576);
for(int i=0; i<imgStr.length; i++){
bt[i] = new JButton(imgStr[i]);
bt[i].setActionCommand(imgStr[i]);
bt[i].addActionListener(this);
btPanel.add(bt[i]);
}
btPanel.setLayout(new FlowLayout());
mainPanel.add(btPanel,"bt");
add(mainPanel);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == null){
eventNo = -1;
}else{
for(int i=0; i<imgStr.length; i++){
if(e.getActionCommand().equals(imgStr[i])){eventNo = i;}
}
}
if(eventNo==-1){
time.stop();
cardLayout.show(mainPanel,"bt");
}
if(eventNo==46){
System.exit(0);
}
if(eventNo>=0){
tracker = new MediaTracker(this);
try{
img = ImageIO.read(new File("./image/001.jpg"));
tracker.addImage(img, 0);
tracker.waitForAll();
canvas = new ImageCanvas(img);
mainPanel.setBounds(0, 0, 1024, 576);
mainPanel.add(canvas,"img");
cardLayout.show(mainPanel,"img");
time.setInitialDelay(5000);
time.start();
}catch(Exception ex){
System.out.println(ex);
}
}
}
}
お礼
回答ありがとうございました