我有一个带有帮助按钮的package com.tv.ravindra;
/**
* Created by Ravindra on 22/01/16.
*/
public class DemoClass extends Activity {
ListView list_demo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
list_demo = (ListView) findViewById(R.id.list_demo);
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("One");
arrayList.add("Two");
arrayList.add("Three");
arrayList.add("Four");
DemoAdapter adapter = new DemoAdapter(DemoClass.this,arrayList);
list_demo.setAdapter(adapter);
}
public class DemoAdapter extends BaseAdapter
{
Activity activity;
ArrayList<String> arrayList = new ArrayList<String>();
public DemoAdapter(Activity activity,ArrayList<String> arrayList)
{
this.activity = activity;
this.arrayList = arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
TextView header_text_tv;
RelativeLayout child_ll;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder _viewHolder;
if (convertView == null) {
_viewHolder = new ViewHolder();
LayoutInflater _layInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = _layInflater.inflate(R.layout.demo_item, null);
_viewHolder.header_text_tv = (TextView)convertView.findViewById(R.id.header_text_tv);
_viewHolder.child_ll = (RelativeLayout)convertView.findViewById(R.id.child_ll);
convertView.setTag(_viewHolder);
} else {
_viewHolder = (ViewHolder) convertView.getTag();
}
_viewHolder.header_text_tv.setText(arrayList.get(position));
for(int i=0;i<=10;i++)
{
TextView msg = new TextView(activity);
msg.setBackgroundColor(activity.getResources().getColor(R.color.bg_blue_color));
msg.setText("Number "+i);
msg.setPadding(10, 10, 10, 10);
msg.setTextColor(getResources().getColor(R.color.white));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 10, 10, 10);
msg.setLayoutParams(params);
_viewHolder.child_ll.addView(msg);
}
return convertView;
}
}
}
。当用户点击“帮助”时,我需要打开一个不同的窗口,其中JMenuBar
显示游戏说明。当用户在其外部点击时,可以关闭该窗口。我想我丢失了代码,因为它不起作用:
Window.java
.jpg
Menu.java
public class Window extends JWindow
{
//java.net.URL imgIntro = getClass().getResource("/images/intro.jpg");
ImageIcon imIntro = new ImageIcon(getClass().getResource("/images/intro.jpg"));
//java.net.URL imgRegles = getClass().getResource("/images/rules.jpg");
ImageIcon imRules = new ImageIcon(getClass().getResource("/images/rules.jpg"));
static Thread t = new Thread();
static int thread = 0;
static JButton bouton;
static int X;
static int Y;
public Window( int X, int Y, int type) {
super();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setSize(X,Y);
setLocation( (dim.width - X)/2 , (dim.height - Y)/2);
setVisible(true);
Container fen = getContentPane();
if (type == 1 ) bouton = new JButton(imIntro);
else bouton = new JButton(imRules);
bouton.setPreferredSize(new Dimension(X, Y) );
fen.add( bouton);
bouton.setVisible( true );
show();
/* if window introduction,
just display for 5 secondes */
if( type == 1 ) {
try {
Thread.sleep(5000);
thread = 1;
}
catch( java.lang.InterruptedException ex ) {
JOptionPane.showMessageDialog(null, "erreur");
}
dispose();
}
/* if window of rules
only close it when user clicks */
else if ( type == 2 ) {
bouton.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}
}
public class Menu extends JMenuBar implements ActionListener{
Interface map;
JMenu m5;
public Menu(Interface map){
super();
this.map=map;
m5 = new JMenu ("Help");//dislay instructions / rules
this.add(m5);
public void actionPerformed(ActionEvent evt){
//.../
else if(evt.getSource () == m5){
//new JWindow
Window rules = new Window( 700, 457, 2);
}
}
}
答案 0 :(得分:1)
我建议您在说明窗口使用JDialog
,而不是JFrame
。例如,请参阅here。
public class Test {
JFrame mainFrame = new JFrame();
JDialog instructions = new JDialog(mainFrame);
public Test() {
gui();
}
public void gui() {
instructions.setSize(200, 200);
JMenuBar mb = new JMenuBar();
JMenu help = new JMenu("Help");
mb.add(help);
JMenuItem instructionsMenu = new JMenuItem("Instructions");
help.add(instructionsMenu);
instructionsMenu.addActionListener(e -> instructions.setVisible(true));
mainFrame.setJMenuBar(mb);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(600, 400);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test());
}
}
备注:强>
setVisible
作为您为窗口做的最后一件事。pack()
而不是设置其大小。答案 1 :(得分:-1)
为什么没有if条件的else-if条件?这会产生语法错误。
另外,显示窗口的代码应该是这样的:
dispose();
Window rules = new Window( 700, 457, 2);
rules.show();