2014-10-20 1 views
0

안녕하세요 저는 게임을 프로그래밍 중이며 둥근 버튼이 멋지다고 생각했지만 궁금한 점은 버튼의 크기를 더 큰 것으로 변경하는 방법입니다. 여기변경 크기가 반올림 된

public JavaApplication3(String label) { 
 
    super(label); 
 
    Dimension size = getPreferredSize(); 
 
    size.width = size.height = Math.max(size.width,size.height); 
 
    setPreferredSize(size); 
 

 
    setContentAreaFilled(false); 
 
    } 
 

 
    protected void paintComponent(Graphics g) { 
 
    if (getModel().isArmed()) { 
 
     g.setColor(Color.RED); 
 
    } else { 
 
     g.setColor(Color.yellow); 
 
    } 
 
    g.fillOval(0, 0, getSize().width-1,getSize().height-1); 
 

 
    super.paintComponent(g); 
 
    } 
 

 
    protected void paintBorder(Graphics g) { 
 
    g.setColor(getForeground()); 
 
    g.drawOval(0, 0, getSize().width-1,  getSize().height-1); 
 
    } 
 

 
    Shape shape; 
 
    public boolean contains(int x, int y) { 
 
    if (shape == null || 
 
     !shape.getBounds().equals(getBounds())) { 
 
     shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight()); 
 
    } 
 
    return shape.contains(x, y); 
 
    }

희망의 사람이 당신이 그것을 클릭 할 때마다 사용하여 버튼의 크기를 변경 :

+0

하십시오 여기

몇 가지 작업 코드? – DreadHeadedDeveloper

답변

0

도움이 될 수 있습니다 내 코드입니다.

addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("klick"); 
      setSize(new Dimension(newSizeWidth, newSizeHeight)); 
     } 
    }); 
} 

그냥

setSize(new Dimension(newSizeWidth, newSizeHeight)); 

메서드를 호출합니다. 아마 당신이 좀 더 그것을 설명 할 수있는, 내가 확실히 코드가 여기에서 무엇을하고 있는지 이해하지

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Shape; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.Ellipse2D; 

import javax.swing.JButton; 
import javax.swing.JFrame; 

public class test2 extends JButton { 

    int width = 100; 
    int height = 100; 

    public test2(String label) { 
     super(label); 

     setSize(width, height); 

     setContentAreaFilled(false); 
     addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("klick"); 
       setSize(new Dimension(width--, height--)); 
      } 
     }); 
    } 

    protected void paintComponent(Graphics g) { 
     if (getModel().isArmed()) { 
      System.out.println(width + " " + height); 

      g.setColor(Color.RED); 
     } else { 
      g.setColor(Color.yellow); 
     } 
     g.fillOval(0, 0, getSize().width - 1, getSize().height - 1); 

     super.paintComponent(g); 
    } 

    protected void paintBorder(Graphics g) { 
     g.setColor(getForeground()); 
     g.drawOval(0, 0, getSize().width - 1, getSize().height - 1); 
    } 

    Shape shape; 

    public boolean contains(int x, int y) { 
     if (shape == null || !shape.getBounds().equals(getBounds())) { 
      shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight()); 
     } 
     return shape.contains(x, y); 
    } 

    static JFrame f; 

    public static void main(String[] args) { 
     f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setLocationRelativeTo(null); 
     f.setSize(200, 200); 
     f.add(new test2("label")); 
     f.setLayout(new BorderLayout()); 
     f.setVisible(true); 
    } 
} 
+0

나는 버튼이 내 코드에서 얼마나 작아지지 않는지와 같은 큰 변화를 의미했다. – coldnorth

+0

글쎄, 나는 네가하는 일을 이해하고 싶었다. 복사/붙여 넣기가 아닙니다. ;-) 당신은 setSize() 메소드로 크기를 변경할 수있다. – huidube