Witam, z pomocą tutorialu na CardLayout zrobiłem prosty aplet z wyborem napisów.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class proba {
public static void main(String[] args) {
JFrame frame = new JFrame("CardLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
JComboBox box = new JComboBox(new String[] { "1", "2", "3" });
CardLayout cardLayout = new CardLayout();
JPanel cards = new JPanel(cardLayout);
cards.add(new MyPanel1(cardLayout), "1");
cards.add(new MyPanel2(cardLayout), "2");
cards.add(new MyPanel3(cardLayout), "3");
box.addItemListener(new Pager(cardLayout, cards));
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(box, BorderLayout.NORTH);
frame.getContentPane().add(cards, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
static class Pager implements ItemListener {
private CardLayout cardLayout;
private JPanel cards;
public Pager(CardLayout cardLayout, JPanel cards) {
this.cardLayout = cardLayout;
this.cards = cards;
}
@Override
public void itemStateChanged(ItemEvent e) {
cardLayout.show(cards, (String) e.getItem());
}
}
}
class MyPanel1 extends JPanel {
private static final long serialVersionUID = 1L;
public MyPanel1(CardLayout cardLayout) {
super(new BorderLayout());
init();
}
private void init() {
JLabel napis = new JLabel("napis1");
add(napis);
setPreferredSize(new Dimension(300, 300));
}
}
class MyPanel2 extends JPanel {
private static final long serialVersionUID = 1L;
public MyPanel2(CardLayout cardLayout) {
super(new BorderLayout());
init();
}
private void init() {
JLabel napis = new JLabel("napis2");
add(napis);
setPreferredSize(new Dimension(300, 300));
}
}
class MyPanel3 extends JPanel {
private static final long serialVersionUID = 1L;
public MyPanel3(CardLayout cardLayout) {
super(new BorderLayout());
init();
}
private void init() {
JLabel napis = new JLabel("napis3");
add(napis);
setPreferredSize(new Dimension(300, 300));
}
}
Ale chciałbym umieścić w poszczególnych kartach rysunki, kod przykładowej figury:
public class figura extends JApplet {
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setBackground(Color.WHITE);
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(1));
Line2D line1 = new Line2D.Double(50, 60, 100, 10);
g2.draw(line1);
Line2D line2 = new Line2D.Double(150, 60, 200, 10);
g2.draw(line2);
Line2D line3 = new Line2D.Double(100, 10, 200, 10);
g2.draw(line3);
Line2D line4 = new Line2D.Double(50, 60, 150, 60);
g2.draw(line4);
Line2D line5 = new Line2D.Double(50, 60, 50, 150);
g2.draw(line5);
Line2D line6 = new Line2D.Double(150, 60, 150, 150);
g2.draw(line6);
Line2D line7 = new Line2D.Double(200, 10, 200, 100);
g2.draw(line7);
Line2D line8 = new Line2D.Double(50, 150, 150, 150);
g2.draw(line8);
Line2D line9 = new Line2D.Double(150, 150, 200, 100);
g2.draw(line9);
float dashPattern[] = {10,5};
g2.setStroke(new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,5,dashPattern,5));
Line2D line10 = new Line2D.Double(100, 10, 100, 100);
g2.draw(line10);
Line2D line11 = new Line2D.Double(100, 100, 50, 150);
g2.draw(line11);
Line2D line12 = new Line2D.Double(100, 100, 200, 100);
g2.draw(line12);
}
}
Kombinowałem ale nie mam pojęcia jak to zrobić, jest to w ogóle możliwe ? Nie jestem jakimś znawcą, Jave mam tylko przez rok w szkole ale fajnie jak by mi to ktoś wytłumaczył jak to zrobić.
Pozdrawiam