Java 中的滚动条 JFrame

发布于 2024-10-28 20:24:17 字数 1630 浏览 5 评论 0原文

我有一个带有“Driver GUI”java 文件的程序,它创建 JFrame 及其规范。

public static void main(String[] args)

  {
     /*Create a frame (outside box) and write what text 
     will be displayed as the frame title*/ 
     JFrame frame = new JFrame("PHILIP MCQUITTY");

     //give frame a size
     frame.setSize(520,375);

     //set location on the computer screen will frame appear
     frame.setLocation(400, 166);

     //use this so when you press X in corner, frame will close
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     //Add your panel to the frame. name must match Panel class name
     frame.setContentPane(new GpaCalc());

        //frame.setResizable(false);

     // always include
     frame.setVisible(true);
  }

该 GUI 调用 .setContentPane 方法并将其连接到我在其中创建了所有 JLable、JButton 和 JTextfield 的资源类。

资源代码片段如下所示:

    public class GpaCalc extends JPanel
   {
      private JLabel GPALabel, c1, c2, c3, c4, c5, c6, c7, g1, g2, g3, g4, g5, g6, g7;
      private JTextField Class1, Class2, Class3, Class4, Class5, Class6, Class7, Grade1, Grade2, Grade3, Grade4, Grade5, Grade6, Grade7;
      private double GPA1, GPA2, GPA3, GPA4, GPA5, GPA6, GPA7, GPA, BigDec;

      public GpaCalc()
      {

         setLayout (new FlowLayout());

         JPanel panel=new JPanel();


      //Class Labels
         GPALabel = new JLabel ("0.00000000000000");
         GPALabel.setFont (new Font("Times New Roman", Font.BOLD, 60));
         GPALabel.setForeground (Color.red);

所以我的问题是,在将 JFrame 的尺寸和 setRessized 设置为 false (.setResizes(false));) 后,如何使 JFrame 垂直滚动?

I have a program with "Driver GUI" java file that creates a JFrame and its specifications.

public static void main(String[] args)

  {
     /*Create a frame (outside box) and write what text 
     will be displayed as the frame title*/ 
     JFrame frame = new JFrame("PHILIP MCQUITTY");

     //give frame a size
     frame.setSize(520,375);

     //set location on the computer screen will frame appear
     frame.setLocation(400, 166);

     //use this so when you press X in corner, frame will close
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     //Add your panel to the frame. name must match Panel class name
     frame.setContentPane(new GpaCalc());

        //frame.setResizable(false);

     // always include
     frame.setVisible(true);
  }

This GUI calls the .setContentPane method and connects it to my resource class where I have created all my JLables, JButtons, and JTextfields.

A snippet of the resource code looks like this:

    public class GpaCalc extends JPanel
   {
      private JLabel GPALabel, c1, c2, c3, c4, c5, c6, c7, g1, g2, g3, g4, g5, g6, g7;
      private JTextField Class1, Class2, Class3, Class4, Class5, Class6, Class7, Grade1, Grade2, Grade3, Grade4, Grade5, Grade6, Grade7;
      private double GPA1, GPA2, GPA3, GPA4, GPA5, GPA6, GPA7, GPA, BigDec;

      public GpaCalc()
      {

         setLayout (new FlowLayout());

         JPanel panel=new JPanel();


      //Class Labels
         GPALabel = new JLabel ("0.00000000000000");
         GPALabel.setFont (new Font("Times New Roman", Font.BOLD, 60));
         GPALabel.setForeground (Color.red);

So my question is, how do I make my JFrame vertically scroll after I set the dimensions of my JFrame and setResizable to false (.setResizable(false));)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜尕妞 2024-11-04 20:24:17

您可以使用 JScrollPane 并将其视图端口设置为您的面板。

You can use JScrollPane for that and set it's view port to your panel.

£冰雨忧蓝° 2024-11-04 20:24:17
frame.setContentPane(new JScrollPane(new GpaCalc())); 
frame.setContentPane(new JScrollPane(new GpaCalc())); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文