16. august 2011 - 10:24
Der er
5 kommentarer og 1 løsning
Paint problem
Goddag folkens, jeg sidder med et lille paint problem, da jeg lige er begyndt at lege med java. I nedslående kode kan jeg ikke få paint til at udskrive begge objekter til applet samtidig kun den sidste bliver udskrevet WHY? Mvh Huutt. / MAIN //***************************************** package test; import java.applet.Applet; public class main extends Applet { food f1; food f2; public void init() { f1 = new bacon(); f2 = new onion(); f1.reload(); f2.reload(); } public static void main(String args[]) { main m = new main(); m.init(); } } // FOOD //***************************************** package test; import java.applet.Applet; import java.awt.Graphics; abstract public class food extends Applet { public void reload() {} public void paint(Graphics g) {} } // BACON OBJECT //***************************************** package test; import java.awt.Color; import java.awt.Graphics; public class bacon extends food { public void reload() { repaint(); } public void paint(Graphics g) { g.fillOval(5,100,300,30); g.setColor(Color.GREEN); g.drawString("Bacon!",100,120); } } // ONION OBJECT //***************************************** package test; import java.awt.Color; import java.awt.Graphics; public class onion extends food { public void reload() { repaint(); } public void paint(Graphics g) { g.fillOval(5,10,300,30); g.setColor(Color.GREEN); g.drawString("Onion!",100,30); } }
Annonceindlæg fra Infor
16. august 2011 - 15:00
#1
Du boer enten lave en Swing app med en main metode eller en JApplet uden main metode. Og selv i tilfaelde af applet skal du kun have en klasse som extender JApplet.
16. august 2011 - 15:06
#2
Simple example with main: import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class GUI1 extends JFrame { private JPanel some; private JButton prt; public GUI1() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(new BorderLayout()); setTitle("GUI #1"); some = new Demo1(); getContentPane().add(some, BorderLayout.CENTER); prt = new JButton("Print"); prt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(GUI1.this, "Printer ikke"); } }); getContentPane().add(prt, BorderLayout.SOUTH); pack(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame f = new GUI1(); f.setVisible(true); } }); } } class Demo1 extends JPanel { public Demo1() { this.setPreferredSize(new Dimension(200,200)); } @Override public void paintComponent(Graphics g) { g.drawString("Det virker OK !!", 20, 20); } }
16. august 2011 - 15:08
#3
Applet eksempel: import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; public class ModalApplet extends JApplet { public void init() { setLayout(new BorderLayout()); getContentPane().add(new JLabel("Test af dialog"), BorderLayout.CENTER); JButton btn1 = new JButton("Åben dialog"); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JDialog dia = new JDialog((Frame)null, "Test", true); dia.setLayout(new BorderLayout()); dia.add(new JLabel("Dette er dialogen"), BorderLayout.CENTER); JButton btn3 = new JButton("Test om dialog er aktiv"); btn3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Dialog er aktiv"); } }); dia.add(btn3, BorderLayout.SOUTH); dia.setMinimumSize(new Dimension(200, 200)); dia.setVisible(true); } }); getContentPane().add(btn1, BorderLayout.NORTH); JButton btn2 = new JButton("Test om applet er aktiv"); btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Applet er aktiv"); } }); getContentPane().add(btn2, BorderLayout.SOUTH); } }
29. august 2011 - 20:41
#4
Sorry, forsinkelsen men takker for svaret arne_v
29. august 2011 - 20:42
#5
og smid så et svar :D
30. august 2011 - 14:36
#6
komme rher
Kurser inden for grundlæggende programmering