16. april 2004 - 20:43
Der er
1 kommentar og
1 løsning
Skift til næste i et cardlayout (Swing)
Hejsa
jeg har oprettet et cardlayout, der skal bestå af en del Jpanels, i bunden af hvert panel er der en frem knap og en tilbage knap.
Hvad skal jeg kode for at få disse knapper til at virke således at det er muligt at bladre frem og tilbage.
Al Swing kode er pt. lavet i en gui klasse.
17. april 2004 - 09:22
#2
eksempel på next og previous:
/*
* CardLayoutDemo.java is a 1.4 application that requires no other files.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo implements ActionListener {
JPanel cards; //a panel that uses CardLayout
final static String BUTTONPANEL = "JPanel with JButtons";
final static String TEXTPANEL = "JPanel with JTextField";
JButton next = new JButton("Next");
JButton prev = new JButton("Previous");
public void addComponentToPane(Container pane) {
//Put the JComboBox in a JPanel to get a nicer look.
JPanel comboBoxPane = new JPanel(); //use FlowLayout
//Create the "cards".
JPanel card1 = new JPanel();
card1.add(new JButton("Button 1"));
card1.add(new JButton("Button 2"));
card1.add(new JButton("Button 3"));
JPanel card2 = new JPanel();
card2.add(new JTextField("TextField", 20));
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);
next.addActionListener(this);
prev.addActionListener(this);
pane.add(prev, BorderLayout.PAGE_START);
pane.add(next, BorderLayout.PAGE_END);
pane.add(cards, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == next)
{
((CardLayout)(cards.getLayout())).next(cards);
}
else if(e.getSource() == prev)
{
((CardLayout)(cards.getLayout())).previous(cards);
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("CardLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
CardLayoutDemo demo = new CardLayoutDemo();
demo.addComponentToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
bygget på det fra suns swing tutorial