Czesc! czy moze ktos mi powiedziec jak wyswietlac 3 rzeczy. Pole tekstowe z wpisywana cyfra calkowita, przycisk zlicz malujacy nam kolo w pozostales czesci okna.
Mój kod:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Grafika extends JFrame implements ActionListener{
JButton wyjscie, zlicz;
JTextField pole;
JLabel kolo;
int prom;
JLabel err;
public Grafika(){
setSize(1200,1000);
setTitle("Kolo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(null);
setVisible(true);
pole = new JTextField();
add(pole);
pole.setBounds(0, 100, 100, 50);
zlicz = new JButton("Wylicz kolo");
add(zlicz);
zlicz.setBounds(0,150,100,50);
zlicz.addActionListener(this);
wyjscie = new JButton("Wyjscie");
add(wyjscie);
wyjscie.setBounds(0,860,100,100);
wyjscie.addActionListener(this);
kolo = new JLabel();
kolo.setBounds(100,0,1100,1000);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == wyjscie)
dispose();
if(source == zlicz){
try{
String promS = pole.getText();
prom = Integer.parseInt(promS);
System.out.println(prom);
add(kolo, new Kolo(prom));
}catch(NumberFormatException ex){
JOptionPane.showMessageDialog(this,"Blędne Dane");
}
}
}
public static void main(String[] args){
new Grafika();
}
}
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JLabel;
public class Kolo extends JLabel{
int prom;
public Kolo(int prom){
this.prom=prom;
repaint();
}
public void paintComponent(Graphics g) {
g.drawOval(200, 0, prom*2, prom*2);
}
}