※ 引述《justbelieve (呆)》之銘言:
: 如題
: 我想按下jbutton
: 會畫一個圓圈(drawOval(.......))在jPanel上
: 可是都畫不上去
: 畫圖的class:
: import java.awt.*;
: import javax.swing.*;
: public class print_picture extends JPanel {
: private String str[]= new String [5];
: public print_picture(){
: super();
: }
: public void paint(Graphics g) {
: super.print(g);
: g.setColor(Color.red);
: g.fillOval(20, 20, 20, 20);
: public static void main(String args[]){
: new print_picture();
: }
: }
: 介面的class:
: button部分:
: jButton1.addActionListener(new java.awt.event.ActionListener() {
: public void actionPerformed(java.awt.event.ActionEvent e) {
: print_picture pict = new print_picture();
: jScrollPane.add(pict);
: }
: });
: jPanel部分:
: private JPanel getJPanel() {
: if (jPanel == null) {
: jPanel = new JPanel();
: jPanel.setLayout(new GridBagLayout());
: }
: return jPanel;
: }
: jScollPane部分:
: private JScrollPane getJScrollPane() {
: if (jScrollPane == null) {
: jScrollPane = new JScrollPane();
: jScrollPane.setBounds(new Rectangle(0, 65, 405, 297));
: jScrollPane.setViewportView(getJPanel());
: }
: return jScrollPane;
: }
: 我是把程式分成兩個class去做
: 介面做一個 畫圖做一個
: 可是要介面這邊的button去接畫圖的class就怎麼也不成功
: 無法把圖畫到jpanel上
: 麻煩知道該怎麼做的大大告知一下
: 謝謝
: PS:這篇有點長,但我絕對不是來騙P幣的,
: 問完如果有人覺得該砍,我會馬上砍掉
你好 因為沒有完整程式碼 看完你的程式碼 我大概猜測一下你怎麼寫
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
class MyPanel extends JPanel{
private int myOffset;
public MyPanel(int offset){
myOffset = offset;
}
public void paintComponent(Graphics g) {
g.setColor(this.getBackground());
g.fillOval(myOffset, 20, 20, 20);
myOffset+=5;
g.setColor(Color.red);
g.fillOval(myOffset, 20, 20, 20);
}
}
class print_picture{
private JButton jButton1;
private JScrollPane jScrollPane;
private MyPanel jPanel;
private JFrame frame;
public print_picture(){
jButton1 = new JButton("jButton1");
jScrollPane = getJScrollPane();
jPanel = getJPanel();
jPanel.add(jButton1);
frame = new JFrame();
frame.getContentPane().add(jScrollPane);
frame.getContentPane().add(jPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
jButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jPanel.repaint();
}
});
}
private MyPanel getJPanel() {
jPanel = new MyPanel(15);
jPanel.setLayout(new GridBagLayout());
return jPanel;
}
private JScrollPane getJScrollPane(){
jScrollPane = new JScrollPane();
jScrollPane.setBackground(Color.blue);
jScrollPane.setBounds(new Rectangle(0, 50, 50, 50));
jScrollPane.setViewportView(jPanel);
return jScrollPane;
}
}
public class WinComponent {
public static void main(String [] args){
new print_picture();
}
}
我加了按一下button橢圓就會移動的方法,方便觀察
發現一些你無法出現圖可能的問題
1.你沒有使用frame,不過我假設是你沒放上來
2.你在這邊弄一個Panel物件
: private JPanel getJPanel() {
: if (jPanel == null) {
: jPanel = new JPanel();
: jPanel.setLayout(new GridBagLayout());
: }
: return jPanel;
: }
又在這邊宣告一個繼承Panel類別的物件
: public void actionPerformed(java.awt.event.ActionEvent e) {
: print_picture pict = new print_picture();
: jScrollPane.add(pict);
: }
所以你畫出來的橢圓應該是會在這個物件上面,不過沒看到你用repaint()
問題應該是出在這邊
其他一些小問題^^"
3.這行我看不出來目的是什麼!可能你有寫其他功能沒放上來
: private String str[]= new String [5];
4.類別最好開頭都是大寫,我只是沿用你的類別名稱
: public class print_picture extends JPanel {
5.最好稱為UI不要稱做介面 我會以為是interface
: 介面做一個 畫圖做一個
以上
如果有一些觀念不正確,請高手指正,因為我還是個新手:P
原po如果還有一些問題可以互相討論^^
謝謝。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.62.165.99