2014-10-05 51 views
2

對於編程類,我被要求創建一個iCalendar應用程序的副本。我使用JAVA來編寫它,並使用JFrame和JPanel來繪製它。這裏是我的問題的SSCCEE:如何將我的JButton移動到我的JPanel的右側?

import java.awt.*; 

import javax.swing.*; 

public class Mainie extends JFrame { 
    private JButton back = new TriangleButton(true), 
      front = new TriangleButton(false); 
    public Mainie(){ 
     super(); 
     setSize(800, 400); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     final JPanel months = new JPanel(); 
     final JPanel days = new JPanel(); 
     JLabel one = new JLabel("Hallo"); 
     one.setHorizontalAlignment(JLabel.CENTER); 
     back.setHorizontalAlignment(SwingConstants.LEFT); 
     front.setHorizontalAlignment(SwingConstants.RIGHT); 
     months.setLayout(new BorderLayout()); 
     months.add(back, BorderLayout.WEST); 
     months.add(one, BorderLayout.CENTER); 
     months.add(front, BorderLayout.EAST); 

     days.setLayout(new GridLayout(1,1)); 
     days.add(new JButton("Meister Camickr")); 

     months.setAlignmentX(CENTER_ALIGNMENT); 
     add(months, BorderLayout.NORTH); 
     add(days, BorderLayout.CENTER); 

     setVisible(true); 
    } 

    public static void main(String[] args){ 
     new Mainie(); 
    } 
} 

class TriangleButton extends JButton { 
    private Shape triangle = createTriangle(); 

    public void paintBorder(Graphics g) { 
     ((Graphics2D) g).draw(triangle); 
    } 

    public void paintComponent(Graphics g) { 
     ((Graphics2D) g).fill(triangle); 
    } 

    public Dimension getPreferredSize() { 
     return new Dimension(200, 100); 
    } 

    public boolean contains(int x, int y) { 
     return triangle.contains(x, y); 
    } 

    public TriangleButton(boolean pointLeft) { 
     super(); 
     if (!pointLeft) 
      this.triangle = createRTriangle(); 
    } 

    private Shape createTriangle() { 
     Polygon p = new Polygon(); 
     p.addPoint(0, 20); 
     p.addPoint(20, 0); 
     p.addPoint(20, 40); 
     return p; 
    } 

    private Shape createRTriangle() { 
     Polygon p = new Polygon(); 
     p.addPoint(20, 20); 
     p.addPoint(0, 0); 
     p.addPoint(0, 40); 
     return p; 
    } 
} 

如果編譯這個,你可以看到我的JButton太遠左側。我怎樣才能把它移到正確的位置?

回答

3

這些按鈕的基本問題是,按鈕非常寬,並且可視指示符位於左側。在他們周圍放置一個邊框,這變得很明顯。

OTOH我會使用完全不同的方法,使用按鈕和工廠方法的文本,使其看起來如預期。

enter image description hereenter image description here

import java.awt.*; 
import javax.swing.*; 

public class CustomPointerButtons { 

    JPanel ui; 

    CustomPointerButtons() { 
     initUI(); 
    } 

    private final void initUI() { 
     ui = new JPanel(new BorderLayout(4, 4)); 

     JPanel topPanel = new JPanel(new BorderLayout(4, 4)); 

     ui.add(topPanel, BorderLayout.PAGE_START); 
     ui.add(new JButton("Mister Mix")); //will default to CENTER 

     topPanel.add(new JLabel("Blah, Blah..")); 
     JButton back = getMinimalButton(new String(Character.toChars(9668))); 
     topPanel.add(back, BorderLayout.LINE_START); 

     JButton forward = getMinimalButton(new String(Character.toChars(9658))); 
     topPanel.add(forward, BorderLayout.LINE_END); 
    } 

    public JButton getMinimalButton(String text) { 
     JButton b = new JButton(text); 
     b.setFont(b.getFont().deriveFont(40f)); 

     b.setMargin(new Insets(0,0,0,0)); 
     b.setContentAreaFilled(false); 
     b.setBorder(null); 

     return b; 
    } 

    public final JComponent getUI() { 
     return ui; 
    } 


    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       JFrame f = new JFrame("Custom Pointer Buttons"); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setContentPane(new CustomPointerButtons().getUI()); 
       f.pack(); 
       f.setLocationByPlatform(true); 
       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
+0

Danke!很棒 – 2014-10-05 06:08:42

0

的問題是你的隨機首選大小並不代表三角形的實際大小。

你可以這樣做:

public Dimension getPreferredSize() { 
//return new Dimension(200, 100); 
Rectangle bounds = triangle.getBounds(); 
return new Dimension(bounds.width, bounds.height); 
} 

您還有其他問題,因爲你仍然有繪畫文物。你應該總是調用super.paintComponent方法()當您覆蓋的方法:

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    ((Graphics2D) g).fill(triangle); 
} 

但是,這仍然沒有真正的工作,因爲按鈕做其他的繪畫也是如此。所以真正的問題是你不應該試圖做按鈕的自定義繪畫。你真正想做的是自定義繪畫Icon。然後,您可以將圖標添加到按鈕。您可能想要查看Playing With Shapes。您可以使用ShapeIcon類來創建圖標。這將提供更靈活的解決方案,因爲我猜所有的形狀都不是由ascii字符表示的。

相關問題