我创建了一个简单的Java程序,并向资源添加了一个简单的JApplet
文件,并创建了一个JApplet
表单。然后我创建了一个名为Main_Draw_JPanel
的简单Java类。我添加到applet窗格,其中一个是类Main_Draw_JPanel
的对象。
我需要此窗格在其上绘制数据结构树。几乎这些树木将以窗格无法全屏显示的方式增长。如何将JScrollbar
添加到此面板?我不知道面板如何用滚动重绘。
这是我的代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.ScrollPane;
import javax.swing.JPanel;
public class Main_Draw_JPanel extends JPanel
{
public void init ( )
{
}
public void paintComponent ( Graphics g )
{
super.paintComponent ( g ) ;
g.setColor ( Color.red ) ;
g.fillRect ( 100 , 100 , 200 , 200 ) ;
g.setColor ( Color.green ) ;
g.fillRect ( 130 , 130 , 140 , 140 ) ;
g.setColor ( Color.yellow ) ;
g.fillRect ( 330 , 330 , 140 , 140 ) ;
}
}
这第二个文件是一个简单的applet表单文件!
import JPanel.Main_Draw_JPanel ;
import java.awt.BorderLayout;
import java.awt.ScrollPane;
import javax.swing.JScrollPane;
public class Main_Tree_JApplet extends javax.swing.JApplet
{
public void init ()
{
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the default
* look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try
{
for ( javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels () )
{
if ( "Nimbus".equals ( info.getName () ) )
{
javax.swing.UIManager.setLookAndFeel ( info.getClassName () );
break;
}
}
}
catch ( ClassNotFoundException ex )
{
java.util.logging.Logger.getLogger ( Main_Tree_JApplet.class.getName () ).log ( java.util.logging.Level.SEVERE , null , ex );
}
catch ( InstantiationException ex )
{
java.util.logging.Logger.getLogger ( Main_Tree_JApplet.class.getName () ).log ( java.util.logging.Level.SEVERE , null , ex );
}
catch ( IllegalAccessException ex )
{
java.util.logging.Logger.getLogger ( Main_Tree_JApplet.class.getName () ).log ( java.util.logging.Level.SEVERE , null , ex );
}
catch ( javax.swing.UnsupportedLookAndFeelException ex )
{
java.util.logging.Logger.getLogger ( Main_Tree_JApplet.class.getName () ).log ( java.util.logging.Level.SEVERE , null , ex );
}
//</editor-fold>
/*
* Create and display the applet
*/
resize ( 1000 , 500 ) ;
//JPanel panel = new JPanel();
//jPanel1.add ( jScrollBar1 ) ;
//add ( sp , "Center" ) ;
//jScrollBar1.
try
{
java.awt.EventQueue.invokeAndWait ( new Runnable ()
{
public void run ()
{
initComponents ();
}
} );
}
catch ( Exception ex )
{
ex.printStackTrace ();
}
}
/**
* This method is called from within the init() method to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always regenerated
* by the Form Editor.
*/
@SuppressWarnings ( "unchecked" )
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
/*
jPanel1 = new javax.swing.JPanel();
*/
jPanel1 = new Main_Draw_JPanel ( ) ;
jPanel2 = new javax.swing.JPanel();
setPreferredSize(new java.awt.Dimension(900, 500));
jPanel1.setBackground(new java.awt.Color(0, 102, 102));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 880, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 371, Short.MAX_VALUE)
);
jPanel2.setBackground(new java.awt.Color(0, 102, 255));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 89, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
}// </editor-fold>
// Variables declaration - do not modify
/*
private javax.swing.JPanel jPanel1;
*/
private Main_Draw_JPanel jPanel1 ;
private javax.swing.JPanel jPanel2;
// End of variables declaration
}
感谢您的回答!