前提・実現したいこと
borderlayoutのcenter部分の幅を狭めたい。
ここに質問の内容を詳しく書いてください。
Calculatorの =??のボタンをもっと上に持ってきてしたに余白を作りたいです。
発生している問題・エラーメッセージ
該当のソースコード
Java
1package Example; 2 3import java.awt.BorderLayout; 4import java.awt.Color; 5import java.awt.Container; 6import java.awt.Dimension; 7import java.awt.GridLayout; 8import javax.swing.BoxLayout; 9import javax.swing.JButton; 10import javax.swing.JFrame; 11import javax.swing.JLabel; 12import javax.swing.JPanel; 13import javax.swing.JSpinner; 14import javax.swing.JTextArea; 15import javax.swing.JTextField; 16import javax.swing.SpinnerNumberModel; 17import javax.swing.border.TitledBorder; 18 19public class MyGui { 20 21 public static void main(String[] args) { 22 //========================================== 23 //Build "person" panel 24 //========================================== 25 JPanel personPanel = new JPanel(); 26 personPanel.setBackground(Color.GREEN); 27 personPanel.setBorder(new TitledBorder("Person Information")); 28 personPanel.setLayout(new BorderLayout()); 29 30 //--------------------------- 31 //Build the Editor sub Panel 32 //--------------------------- 33 JPanel personEditor = new JPanel(); 34 personEditor.setBackground(Color.LIGHT_GRAY); 35 personEditor.setLayout(new GridLayout(3, 1)); 36 37 JLabel first = new JLabel("First Name : "); 38 JLabel last = new JLabel("Last Name : "); 39 JLabel id = new JLabel("ID# : "); 40 41 JTextField firstTxt = new JTextField(10); 42 JTextField lastTxt = new JTextField(10); 43 JTextField idTxt = new JTextField(10); 44 45 personEditor.add(first); 46 personEditor.add(firstTxt); 47 personEditor.add(last); 48 personEditor.add(lastTxt); 49 personEditor.add(id); 50 personEditor.add(idTxt); 51 52 //attached the sub panel editor to the person panel 53 personPanel.add(personEditor, BorderLayout.NORTH); 54 55 //--------------------------- 56 //Build the Button sub Panel 57 //--------------------------- 58 JPanel personButtons = new JPanel(); 59 personButtons.setBackground(Color.CYAN); 60 61 JButton createPerson = new JButton("Add"); 62 personButtons.add(createPerson); 63 JButton deletePerson = new JButton("Remove"); 64 personButtons.add(deletePerson); 65 66 //attached the sub panel editor to the person panel 67 personPanel.add(personButtons, BorderLayout.SOUTH); 68 69 //--------------------------- 70 //Build the Display sub Panel 71 //--------------------------- 72 JPanel display = new JPanel(); 73 display.setBackground(Color.MAGENTA); 74 personPanel.add(display, BorderLayout.CENTER); 75 76 //========================================== 77 //Build "output" panel 78 //========================================== 79 JPanel outputPanel = new JPanel(); 80 outputPanel.setBackground(Color.YELLOW); 81 outputPanel.setBorder(new TitledBorder("Display Results")); 82 outputPanel.setLayout(new BorderLayout()); 83 84 //--------------------------- 85 //Build the Button sub Panel 86 //--------------------------- 87 JPanel outputButtons = new JPanel(); 88 outputButtons.setBackground(Color.ORANGE); 89 90 JButton drawMatrix = new JButton("Draw Matrix"); 91 outputButtons.add(drawMatrix); 92 93 //attached the sub panel editor to the output panel 94 outputPanel.add(outputButtons, BorderLayout.SOUTH); 95 96 //--------------------------- 97 //Build the Result sub Panel 98 //--------------------------- 99 JTextArea outputArea = new JTextArea(); 100 outputPanel.add(outputArea, BorderLayout.CENTER); 101 102// 103 //========================================== 104 //Build "calculate" panel 105 //========================================== 106 JPanel calculatePanel = new JPanel(); 107 calculatePanel.setBackground(Color.PINK); 108 calculatePanel.setBorder(new TitledBorder("Calculator")); 109 calculatePanel.setLayout(new BorderLayout()); 110 111 //--------------------------- 112 //Build the spinner 113 //--------------------------- 114 JPanel calculateSpinner = new JPanel(); 115 calculateSpinner.setBackground(Color.RED); 116 calculateSpinner.setLayout(new GridLayout(2, 1)); 117 118 JSpinner sp1 = new JSpinner(); 119 calculateSpinner.add(sp1); 120 sp1.getValue(); 121 122 JSpinner sp2 = new JSpinner(); 123 calculateSpinner.add(sp2); 124 sp2.getValue(); 125 126 calculatePanel.add(calculateSpinner, BorderLayout.NORTH); 127 //--------------------------- 128 //Build the + - Button 129 //--------------------------- 130 JPanel calculateButtons = new JPanel(); 131 calculateButtons.setBackground(Color.LIGHT_GRAY); 132 Dimension d = new Dimension(30, 50); 133 calculateButtons.setSize(d); 134 135 JButton plus = new JButton("+"); 136 calculateButtons.add(plus); 137 138 JButton minas = new JButton("-"); 139 calculateButtons.add(minas); 140 141 //attached the sub panel editor to the calculate panel 142 calculatePanel.add(calculateButtons, BorderLayout.CENTER); 143 //calculatePanel.setPreferredSize(new Dimension(200, 50)); 144 145 //--------------------------- 146 //Build the = ?? Button 147 //--------------------------- 148 JPanel totalButtons = new JPanel(); 149 150 totalButtons.setBackground(Color.RED); 151 152 JButton total = new JButton("= ??"); 153 154 totalButtons.add(total); 155 156 //attached the sub panel editor to the calculate panel 157 calculatePanel.add(totalButtons, BorderLayout.SOUTH); 158 159 //========================================== 160 //My window, and it packs everyting together 161 //========================================== 162 JFrame window = new JFrame(); 163 164 window.setTitle("GUI with no ActionListeners"); 165 166 window.add(personPanel, BorderLayout.WEST); 167 168 window.add(outputPanel, BorderLayout.CENTER); 169 170 window.add(calculatePanel, BorderLayout.EAST); 171 172 //set the size to described dimensions 173// Dimension d = new Dimension(455, 500); 174// window.setSize(d); 175 176 //calculating layout and sizes everything fits 177 window.pack(); 178 179 //show it! 180 window.setVisible(true); 181 182 //end the program and this window closes properly 183 window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 184 185 }//end of main 186}//end of class 187
試したこと
Dimension d = new Dimension(30, 50);
calculateButtons.setSize(d);や
calculatePanel.setPreferredSize(new Dimension(200, 50));の挿入
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー