2016-09-13 6 views
-1

編集内容は下記をご覧ください。JFrameを使用してJavaボタンの位置を設定しようとしていませんか?

私は私の問題を解決するために多数の "解決策"を見てきましたが、私はそれを働かせることはできません。

これは私のアプリケーションは以下のコードに次のようになります。

enter image description here

基本的に、私は、ボタンの位置を設定したいが、私はそうすることを管理することはできません。ここに私のコードです:

package me.cervinakuy.application; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridLayout; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class ControlPanel3 extends JFrame { 

    JPanel panel = new JPanel(); 

    JButton startRobo = new JButton(); 
    JButton stopRobo = new JButton(); 
    JButton restartRobo = new JButton(); 

    public ControlPanel3() { 

//  setLayout(null); 
     setSize(1000, 700); 
     setResizable(false); 
     setLocation(450, 150); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     getContentPane().setBackground(new Color(45, 48, 55)); 
     setTitle("Espin Software | Control Panel"); 
     setVisible(true); 

     startRobo.setIcon(new ImageIcon(getClass().getResource("/resources/startRobo.png"))); 
     stopRobo.setIcon(new ImageIcon(getClass().getResource("/resources/stopRobo.png"))); 
     restartRobo.setIcon(new ImageIcon(getClass().getResource("/resources/restartRobo.png"))); 

     startRobo.setBorder(null); 
     stopRobo.setBorder(null); 
     restartRobo.setBorder(null); 

     startRobo.setLocation(100, 100); 

     panel.add(startRobo); 
     panel.add(stopRobo); 
     panel.add(restartRobo); 
     panel.setOpaque(false); 

     add(panel); 

     validate(); 

    } 

} 

編集:私は今、私が最初に探していたもののGUIを作成するために管理している 、しかし、私は新しい問題を抱えています。ボタンは、画像だけでなく、GUIの異なる部分から押されるようになりました。興味のある方のために、ここで私は達成することができたものです:

New GUI look.

更新されたコード:

package me.cervinakuy.application; 

import java.awt.Color; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class ControlPanel3 extends JFrame { 

    JPanel panel = new JPanel(); 

    JButton startRobo = new JButton(); 
    JButton stopRobo = new JButton(); 
    JButton restartRobo = new JButton(); 

    public ControlPanel3() { 

//  setLayout(null); 
     setSize(1000, 700); 
     setResizable(false); 
     setLocation(450, 150); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     getContentPane().setBackground(new Color(45, 48, 55)); 
     setTitle("Espin Software | Control Panel"); 
     setVisible(true); 

     startRobo.setIcon(new ImageIcon(getClass().getResource("/resources/startRobo.png"))); 
     stopRobo.setIcon(new ImageIcon(getClass().getResource("/resources/stopRobo.png"))); 
     restartRobo.setIcon(new ImageIcon(getClass().getResource("/resources/restartRobo.png"))); 

     startRobo.setBorder(null); 
     stopRobo.setBorder(null); 
     restartRobo.setBorder(null); 

     panel.setLayout(null); 
     startRobo.setLocation(200, 200); 
     startRobo.setBounds(5, -95, 300, 300); 
     stopRobo.setBounds(5, 0, 300, 300); 
     restartRobo.setBounds(5, 95, 300, 300); 

     panel.add(startRobo); 
     panel.add(stopRobo); 
     panel.add(restartRobo); 
     panel.setOpaque(false); 

     add(panel); 

     validate(); 

    } 

} 
+3

*)トップ5に*リンク「私の問題を解決するために、 『&彼らはA)を支援するために失敗した理由を説明するソリューション』私は、多くのを見ているしました」、&bが明示的な位置を設定することを説得することができませんでした部品の生産性が低下しました。 –

+1

1)アスキーアート、またはGUIの*意図された*レイアウトの簡単な図面を最小サイズで提供し、サイズ変更可能な場合は幅と高さを増やしてください。 2)すぐに助けを得るために、[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿してください。 3)画像を取得する1つの方法は、[このQ&A](http://stackoverflow.com/q/19209650/418556)に表示されている画像にホットリンクすることです。 –

+0

私はすでに質問を表題にしたときに登場した最初の5つを試しましたが、最初の数は私が達成しようとしているものに関連していませんでした。私が数多くのソリューションを見てきたと言いますと、私は持っています。 意図するレイアウトはかなり単純で、左に揃えてあります。しかし、何らかの理由で、彼らはすべて中心に置かれ、一番上に置かれます。私は場所を設定する方法を見つけようとしています。場所がなくなったので、この質問の目的です。 – Espin

答えて

3

enter image description here

レイアウトにいくつかの方法が一般的にあります。同じ効果で終わるコンポーネント。この例では、列内のボタン(buttonContainerGridLayoutを使用)を含むパネルを使用し、次にそのコンテナを上部(buttonConstrainPanelを使用してBorderLayoutを使用)に制限するパネルを使用し、そのパネルを左側に配置するコンテナを使用します。uiBorderLayout)。

単一のGridBagLayoutまたはGroupLayoutを使用して達成することもできますが、それを達成するロジックは単純ではないかもしれません。

青いボタンに表示されるフォーカス枠は、マウスをクリックするとボタンがアクティブになる場所の限界を示します。

import java.awt.*; 
import java.net.MalformedURLException; 
import java.net.URL; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

public class ThreeButtonAlignedLeft { 

    private JComponent ui = null; 
    private String prefix = "http://i.stack.imgur.com/"; 
    private String[] suffix = {"gJmeJ.png","T5uTa.png","wCF8S.png"}; 

    ThreeButtonAlignedLeft() { 
     try { 
      initUI(); 
     } catch (MalformedURLException ex) { 
      ex.printStackTrace(); 
     } 
    } 

    public void initUI() throws MalformedURLException { 
     if (ui!=null) return; 

     ui = new JPanel(new BorderLayout(4,4)); 
     ui.setBorder(new EmptyBorder(4,4,4,4)); 

     JPanel buttonContainer = new JPanel(new GridLayout(0, 1, 5, 5)); 
     for (int ii=0; ii<suffix.length; ii++) { 
      JButton b = new JButton(new ImageIcon(new URL(prefix + suffix[ii]))); 
      b.setBorderPainted(false); 
      b.setMargin(new Insets(0,0,0,0)); 
      b.setContentAreaFilled(false); 
      buttonContainer.add(b); 
     } 
     JPanel buttonConstrainPanel = new JPanel(new BorderLayout(0, 0)); 
     buttonConstrainPanel.add(buttonContainer, BorderLayout.PAGE_START); 

     ui.add(buttonConstrainPanel, BorderLayout.LINE_START); 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception useDefault) { 
       } 
       ThreeButtonAlignedLeft o = new ThreeButtonAlignedLeft(); 

       JFrame f = new JFrame(o.getClass().getSimpleName()); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
+1

驚異的な「ヌル」レイアウトは実際には機能しません! –

関連する問題