在我的程序中,我将我的元素组织起来叠加在一起,这里唯一的问题是它们水平拉伸以触摸JFrame
的所有边缘,我不想要这个而且不能用于我的生活弄清楚如何阻止这种情况发生。
以下是我的代码,非常感谢。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
@SuppressWarnings({ "serial", "unused" })
public class MedGUITest extends JFrame {
private JLabel medName;
private JLabel medTime;
private JLabel medDose;
private JLabel finished;
private JTextField textFieldMed;
private JTextField textFieldTime;
private JTextField textFieldDose;
private JButton submitButton;
private JButton meds;
static int winWidth = 300;
static int winLength = 300;
JTextArea ta = new JTextArea(winWidth,winLength);
JPanel panel = new JPanel();
public MedGUITest() throws FileNotFoundException, IOException{
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
add(panel);
setLayout(new FlowLayout());
medName = new JLabel("Type Medication Here:");
panel.add(medName);
textFieldMed = new JTextField("",15);
panel.add(textFieldMed);
medDose = new JLabel("Type Medication Dose:");
panel.add(medDose);
textFieldDose = new JTextField("",15);
panel.add(textFieldDose);
medTime = new JLabel("Type Medication Time:");
panel.add(medTime);
textFieldTime = new JTextField("",15);
panel.add(textFieldTime);
submitButton = new JButton("Click Here to Add");
submitButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
String medName = textFieldMed.getText();
String medDose = textFieldDose.getText();
String medTime = textFieldTime.getText();
File med = new File("med.txt");
try(PrintWriter out= new PrintWriter(new FileWriter(med,true))) {
out.println("Medication: " + medName + " " +"Dosage: "+ medDose + " Mg"+ " " +"Time of day: "+ medTime);
String lastMed = ("Medication: " + medName + " " +"Dosage: "+ medDose + " Mg"+ " " +"Time of day: "+ medTime+ "\n");
finished.setText("Your Med has been added");
Timer t = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
finished.setText(null);
}
});
t.setRepeats(false);
t.start();
ta.append(lastMed);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
panel.add(submitButton);
final File med = new File("med.txt");
if(med.exists()){
}else{
meds = new JButton("Click Here to see meds");
meds.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(med.exists()){
try {
ta.read(new FileReader("med.txt"),null);
} catch (IOException e1) {
e1.printStackTrace();
}
panel.add(ta);}
}
});
panel.add(meds);
}
finished = new JLabel("");
panel.add(finished);
if(med.exists()){
ta.read(new FileReader("med.txt"),null);
panel.add(ta);}
}
public static void main(String args[]) throws FileNotFoundException, IOException{
MedGUITest gui = new MedGUITest();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.pack();
gui.setVisible(true);
gui.setTitle("Standard GUI");
}
}
答案 0 :(得分:2)
尝试将EmptyBorder
添加到内容窗格...
getContentPane().setBorder(new EmptyBorder(10, 10, 10, 10));
您还可以考虑使用提供"填充"的布局管理器,个人而言,我更喜欢GridBagLayout
,但您也可以查看FlowLayout(int align, int hgap, int vgap)
,但这样将影响所有组件之间的间距,而不仅仅是框架
请查看How to Use Borders和Laying Out Components Within a Container以获取更多想法