How to make a layout for JApplet

你离开我真会死。 提交于 2020-01-04 12:49:08

问题


I am creating a simple Sudoku game. Since this is my first "big" i want to do everything myself (without the NetBeans interface designer that i normally use to make GUIs). So for the GUI i created a class that extends JApplet and i drew a simple sudoku field in the paint() method.

Now i need to make 81 text fields each of which will take in 1 number. How do i position them on the screen? Also, i was thinking of making an array so i'll be able to change the enitre matrix of fields with one for loop.


回答1:


Suggestions:

  • Never draw directly in the paint method of a top-level component such as a JApplet, JFrame, JDialog, or the like.
  • If you need to do custom drawing, do this instead in the paintComponent(...) method override of a component that extends JComponent such as a JPanel or JComponent itself.
  • Your problem doesn't really require custom painting, at least not yet, and is much better and more simply solved by other means.
  • Use components such as JLabels, JTextFields, etc... and position them using the Swing layout managers (the other means noted above). You can find the tutorial on how to use this here: Laying Out Components in a Container
  • Layouts to focus on first include GridLayout for your Sudoku "cells" and BorderLayout for the overall GUI. Avoid the GridBagLayout and GroupLayout, at least when starting out.
  • Remember that you can create complex applications by nesting JPanels that each use a simple layout manager.
  • A simple way to "paint" gridlines is to set the background color of the the JPanel that uses GridLayout and holds the JTextFields to Color.BLACK, and be sure to give your GridBagLayout a small vertical and horizontal gap so that the black shows through. The tutorials listed above will show you how to do this.
  • If this were my application, I'd gear my GUI towards creating a JPanel that held the application. Then if I needed to display it in a JApplet, I'd create a very small application that subclasses JApplet and then in the init() method, add my Sudoku JPanel into the JApplet's contentPane. This way, if I wanted to instead display my app in a JFrame, all I'd need to do would be to create another small class that creates a JFrame and add's my Sudoku JPanel into the JFrame's contentPane, then call pack() on the JFrame, and then setVisible(true).

Regarding your question on how to add a JPanel to a JApplet, again the tutorials will show you how to do this. If you haven't linked to the tutorial's big index, you will want to do so: The Really Big Index.

A very simple example goes like so:

import java.lang.reflect.InvocationTargetException;
import javax.swing.*;

public class MyApplet extends JApplet {
   @Override
   public void init() {
      try {
         SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               getContentPane().add(new MyJPanel());             
            }
         });
      } catch (InterruptedException e) {
         e.printStackTrace();
      } catch (InvocationTargetException e) {
         e.printStackTrace();
      }
   }
}


来源:https://stackoverflow.com/questions/9342334/how-to-make-a-layout-for-japplet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!